# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1017334 | khanhtb | Feast (NOI19_feast) | C++14 | 113 ms | 12180 KiB |
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 ll long long
#define ull unsigned long long
#define ld double
#define pb push_back
#define pf push_front
#define vi vector<ll>
#define vll vector<vi>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define all(a) a.begin(), a.end()
#define fi first
#define se second
using namespace std;
const ll mod = 1e9+7;
const ll inf = 2e18;
const ll base = 31;
const ll blockdeg = 320;
const ll N = 3e5+8;
ll n,k,a[N];
pll dp[N][2];
pll solve_lambda(ll m){
dp[1][0] = {0,0};
dp[1][1] = {a[1]-m,1};
for(ll i = 2; i <= n; i++){
dp[i][0] = max(dp[i-1][0],dp[i-1][1]);
dp[i][1] = max(make_pair(dp[i-1][0].fi-m+a[i],dp[i-1][0].se+1),make_pair(dp[i-1][1].fi+a[i],dp[i-1][1].se));
}
return max(dp[n][0],dp[n][1]);
}
void solve(){
cin >> n >> k;
for(ll i = 1; i <= n; i++) cin >> a[i];
ll l = 0, r = 1e18;
pll ans;
ll lbd;
while(l <= r){
ll m = l+r>>1;
pll s = solve_lambda(m);
if(s.se <= k){
ans = s;
lbd = m;
r = m-1;
}
else l = m+1;
}
cout << ans.fi+lbd*k;
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
if(fopen("test.inp","r")){
freopen("test.inp","r",stdin);
freopen("test.out","w",stdout);
}
ll T = 1;
// cin >> T;
for(ll i = 1; i <= T; i++){
solve();
// cout << '\n';
}
}
Compilation message (stderr)
# | 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... |