Submission #1168977

#TimeUsernameProblemLanguageResultExecution timeMemory
1168977tnknguyen_Feast (NOI19_feast)C++20
100 / 100
107 ms12244 KiB
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define ll long long #define len(s) (int)s.size() #define int long long #define pii pair<int, int> #define fi first #define se second #define MASK(k) (1LL << (k)) int n, k; const int MX = 3e5 + 5; int a[MX]; pii dp[MX][2]; pii solve(int lmb){ // {sum with current penalty, # of arrays} dp[1][0] = {0, 0}; //do not create new array dp[1][1] = {a[1] - lmb, 1}; // create 1 array with penalty for(int i=2; i<=n; ++i){ // do not do anything dp[i][0] = max(dp[i-1][0], dp[i-1][1]); // create new array with penalty or assign to previous array pii p1 = {dp[i-1][0].fi + a[i] - lmb, dp[i-1][0].se + 1}; pii p2 = {dp[i-1][1].fi + a[i], dp[i-1][1].se}; dp[i][1] = max(p1, p2); } return max(dp[n][0], dp[n][1]); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("main.inp","r",stdin); // freopen("main.out","w",stdout); cin >> n >> k; for(int i=1; i<=n; ++i) cin >> a[i]; int l = 0, r= 1e18; while(l < r){ int mid = (l + r + 1) / 2; pii cost = solve(mid); if(cost.se >= k) l = mid; else r = mid - 1; } cout << solve(l).fi + l*k; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...