이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define S second
#define F first
#define pb push_back
using namespace std;
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
long long n, k, a[N], pre[N];
vector<pair<long long, int> > dp(N), cur_dp(N);
long long C(int i, int j){
return (pre[i] - pre[j]) * pre[j];
}
void compute(int l, int r, int tl, int tr){
if(l > r)
return;
int mid = (l + r) / 2;
pair<long long, int> p = {INT_MIN, -1};
for(int i = tl; i <= min(mid - 1, tr); ++i){
if(dp[i].F + C(mid, i) > p.F){
p.F = dp[i].F + C(mid, i);
p.S = i;
}
}
if(p.S == -1) p.S = mid;
cur_dp[mid] = p;
compute(l, mid - 1, tl, p.second);
compute(mid + 1, r, p.second, tr);
}
void solve(){
cin >> n >> k;
for(int i = n - 1; i >= 0; --i)
cin >> a[i];
pre[0] = a[0];
for(int i = 1; i < n; ++i)
pre[i] = pre[i - 1] + a[i];
for(int i = 1; i <= k; ++i){
compute(0, n - 1, 0, n - 1);
for(int j = 1; j < n; ++j){
if(cur_dp[j - 1].F > cur_dp[j].F)
cur_dp[j] = cur_dp[j - 1];
}
dp = cur_dp;
}
cout << dp[n - 1].F << '\n';
int pos = n - 1, ct = k;
while(ct > 0){
cout << n - dp[pos].S - 1 << ' ';
pos = dp[pos].S;
--ct;
}
}
int main(){
fast;
int t = 1;
// cin >> t;
while(t--)
solve();
return 0;
}
# | 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... |