이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> a;
pair<ll, int> calc(ll bm){
int n = a.size();
pair<ll, int> pfmax(0, 0);
pair<ll, int> prevdp(0, 0);
for (int i=0; i<n; i++){
// dp[i] = max(dp[i-1], dp[k-1] + pf[i] - pf[k-1] - bm)
// (dp[k-1] - pf[k-1]) + pf[i] - bm
pair<ll, int> cdp = prevdp;
cdp = max(cdp, pair<ll, int> (pfmax.first + a[i] - bm,
pfmax.second + 1));
pfmax = max(pfmax, pair<ll, int> (cdp.first - a[i], cdp.second));
prevdp = cdp;
}
return prevdp;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n, k; cin >> n >> k;
a.resize(n);
for (int i=0; i<n; i++) cin >> a[i];
partial_sum(a.begin(), a.end(), a.begin());
ll bl=0, br=1e18;
vector<pair<ll, int>> dp(n);
while (bl < br){
ll bm = (bl + br) >> 1;
if (calc(bm).second > k) bl = bm+1;
else br = bm;
}
pair<ll, int> s = calc(bl);
cout << s.first + s.second * bl << "\n";
}
# | 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... |