# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1017410 | vjudge1 | Feast (NOI19_feast) | C++17 | 107 ms | 12132 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>
using namespace std;
#define int long long
pair<int, int> dp[300005][2];
int n, k, a[300005];
pair<int, int> sigma(int cost) {
dp[0][1] = {-1e18, 0};
for (int i = 1; i <= n; i++) {
dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
pair<int,int> v1 = {dp[i-1][1].first+a[i],dp[i-1][1].second};
pair<int,int> v2 = {dp[i-1][0].first+a[i]-cost, dp[i-1][0].second+1};
dp[i][1] = (v1.first > v2.first ? v1 : v2);
//if(cost == 2) cout << i << ' ' << dp[i][0].first << ' ' << dp[i][0].second << ' ' << dp[i][1].first << ' ' << dp[i][1].second << '\n';
}
return max(dp[n][0], dp[n][1]);
}
void solve() {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
int l = 0, r = 3e14, mid, ans=0;
while(l <= r) {
mid = (l + r) / 2;
pair<int, int> val = sigma(mid);
if(val.second <= k) {
// cout << mid << ' ' << val.first << ' ' << val.second << '\n';
ans = max(ans, val.first + mid * val.second);
r = mid-1;
}
else l = mid+1;
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen("input.inp","r")){
freopen("input.inp", "r", stdin);
freopen("output.out", "w", stdout);
}
int t = 1;
// cin >> t;
while(t--) {
solve();
}
}
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... |