Submission #1122213

#TimeUsernameProblemLanguageResultExecution timeMemory
1122213minggaFeast (NOI19_feast)C++17
100 / 100
149 ms15228 KiB
#include "bits/stdc++.h" using namespace std; #define ln "\n" #define pb push_back #define fi first #define se second #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define int long long const int mod = 1e9 + 7; const int inf = 2e18; const int N = 3e5 + 7; int n, k, a[N]; pair<int, int> dp[N][2]; pair<int, int> calc(int pen) { dp[1][0] = {0, 0}; dp[1][1] = {a[1] - pen, 1}; for(int 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][1].fi + a[i], dp[i - 1][1].se), make_pair(dp[i - 1][0].fi + a[i] - pen, dp[i - 1][0].se + 1)); } return max(dp[n][0], dp[n][1]); } signed main() { cin.tie(0) -> sync_with_stdio(0); cin >> n >> k; for(int i = 1; i <= n; i++) cin >> a[i]; dp[0][0] = dp[0][1] = {0, 0}; int l = 0, r = 1e18; int ans = 0; while(l <= r) { int m = (l + r) >> 1; if(calc(m).se >= k) { ans = m; l = m + 1; } else r = m - 1; } cout << calc(ans).fi + k *ans; cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC; }
#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...