이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll>pii;
typedef pair<pii,pii>piii;
const int mxn = 1e4+5;
#define inf 1e16
#define all(v) v.begin(), v.end()
#define FAST ios_base::sync_with_stdio(0);cout.tie(0)
ll arr[mxn];
ll dp[mxn][205];
ll pre[mxn];
int dir[mxn][205];
int n, k;
ll solve(int pos, int x)
{
if(pos>n) {
return -inf;
}
if(x==k) return 0;
if(dp[pos][x]!=-1) return dp[pos][x];
ll sum = 0, ret = -inf;
for(int i = pos; i <= n; i++) {
sum+=arr[i];
ll tmp = max(ret, solve(i+1, x+1)+(sum*pre[i+1]));
if(tmp>ret) {
ret = tmp;
dir[pos][x] = i+1;
}
}
return dp[pos][x] = ret;
}
void pathprint(int pos, int x)
{
if(x==k) return;
cout << dir[pos][x]-1 << ' ';
pathprint(dir[pos][x], x+1);
}
int main()
{
FAST;
cin >> n >> k;
for(int i = 1; i <= n; i++) {
cin >> arr[i];
}
pre[n] = arr[n];
for(int i = n-1; i >= 1; i--) {
pre[i] = pre[i+1]+arr[i];
}
memset(dp, -1, sizeof dp);
ll ans = solve(1, 0);
cout << ans << '\n';
pathprint(1, 0);
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... |