이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
const int N = 1e3 + 5;
const int M = 205;
int a[N],n,k;
ll pre[N],dp[N][M];
ll get(int l,int r) {
return pre[r] - pre[l - 1];
}
ll f(int x,int rem) {
auto &res = dp[x][rem];
if (res != -1)
return res;
if (x == n + 1 || rem == 0)
return res = 0;
for (int i = x + 1 ; i <= n ; i++) {
auto temp = f(i,rem - 1);
res = max(res,get(i,n) * get(x,i - 1) + temp);
}
return res;
}
void write(int x,int rem) {
if (x == n + 1 || rem == 0)
return ;
for (int i = x + 1 ; i <= n ; i++) {
auto temp = dp[i][rem - 1];
if (dp[x][rem] == get(i,n) * get(x,i - 1) + temp) {
cout << i - 1 << " ";
write(i,rem - 1);
break;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> n >> k;
for (int i = 1 ; i <= n ; i++) {
cin >> a[i];
pre[i] = pre[i - 1] + a[i];
}
memset(dp,-1,sizeof dp);
cout << f(1,k) << "\n";
write(1,k);
}
# | 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... |