# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
48489 | zadrga | Split the sequence (APIO14_sequence) | C++14 | 410 ms | 10720 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF (1LL << 55)
#define MOD (1000 * 1000 * 1000 + 7)
#define maxn 1111
#define maxk 211
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
ll dp[maxn][maxn], pre[maxn], arr[maxn];
int from[maxn][maxk];
ll sum(int i, int j){
if(i > j)
return 0LL;
return pre[j] - pre[i - 1];
}
int main(){
int n, k;
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++){
scanf("%lld", arr + i);
pre[i] = pre[i - 1] + arr[i];
}
for(int i = 0; i <= n; i++)
for(int j = 0; j <= k; j++)
dp[i][j] = -INF;
for(int i = 1; i <= n - 1; i++)
dp[i][1] = sum(1, i) * sum(i + 1, n);
for(int j = 2; j <= k; j++){
for(int i = j; i <= n - 1; i++){
// cout << "dp " << i << " "<< j << ": \n";
// cout << dp[i][j] << endl;
for(int z = 1; z < i; z++){
// cout << "vrednost " << z <<": " <<sum(i + 1, n) << " " << (sum(1, i) - sum(1, z)) << " " <<dp[z][j - 1] << endl;
if(dp[i][j] < sum(i + 1, n) * (sum(1, i) - sum(1, z)) + dp[z][j - 1]){
// cout << dp[i][j] << " " << z << endl;
dp[i][j] = sum(i + 1, n) * (sum(1, i) - sum(1, z)) + dp[z][j - 1];
from[i][j] = z;
}
}
// cout << dp[i][j] << endl << endl;
}
}
/* for(int i = 0; i <= k; i++){
for(int j = 0; j <= n; j++)
printf("%d ", from[j][i]);
printf("\n");
} */
int ans = 0;
for(int i = 1; i <= n - 1; i++){
if(dp[i][k] > dp[ans][k])
ans = i;
}
printf("%lld\n", dp[ans][k]);
for(int i = k; i >= 1; i--){
printf("%d ", ans);
ans = from[ans][i];
}
return 0;
}
Compilation message (stderr)
# | 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... |