This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(x) ((int) (x).size())
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vp = vector<pii>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vb = vector<bool>;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p){return os << '(' << p.fi << ", " << p.se << ')';}
template<typename C, typename T = typename enable_if<!is_same<C, string>::value, typename C::value_type>::type>
ostream& operator<<(ostream &os, const C &v){string sep; for(const T &x : v) os << sep << x, sep = " "; return os;}
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values){
cout << "[Debug]\n\t" << vars << " = ";
string d = "[";
(..., (cout << d << values, d = "] ["));
cout << "]\n";
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int n,k;
cin >> n >> k;
vl a(n + 1);
for(int i=1; i<=n; i++) cin >> a[i];
auto DP = [&](ll lambda) -> array<ll,2> {
vector< array<ll,2> > dp(n + 1), dp2(n + 1);
for(int i=1; i<=n; i++){
dp[i][0] = dp[i-1][0];
dp2[i][0] = dp2[i-1][0];
if(a[i] - lambda + dp[i-1][1] > dp[i][0]){
dp[i][0] = a[i] - lambda + dp[i-1][1];
dp2[i][0] = 1 + dp2[i-1][1];
}
dp[i][1] = dp[i][0];
dp2[i][1] = dp2[i][0];
if(a[i] + dp[i-1][1] >= dp[i][1]){
dp[i][1] = a[i] + dp[i-1][1];
dp2[i][1] = dp2[i-1][1];
}
}
return {dp[n][0], dp2[n][0]};
};
ll ini = 0, fin = 1e18, med;
array<ll,2> ans;
while(ini < fin){
med = (ini + fin)/2;
ans = DP(med);
if(ans[1] > k) ini = med + 1;
else fin = med;
}
ans = DP(ini);
cout << ans[0] + ini*ans[1] << "\n";
return 0;
}
Compilation message (stderr)
feast.cpp: In function 'void logger(std::string, Args&& ...)':
feast.cpp:27:42: warning: fold-expressions only available with '-std=c++17' or '-std=gnu++17'
27 | (..., (cout << d << values, d = "] ["));
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |