이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define sz(x) (int)x.size()
#define F first
#define S second
#define int long long
const int maxn = 1e4+7;
const int inf = 1e18;
const int mod = 1e9+7;
using namespace std;
int n, k;
int a[maxn];
int pref[maxn];
int dp[210][maxn];
int p[210][maxn];
void solve(){
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
}
for(int i = 1; i <= n; i++) dp[0][i] = -inf;
for(int i = 1; i <= k + 1; i++){
for(int j = 1; j <= n; j++){
int res = -inf;
for(int pos = 0; pos < j; pos++){
if(dp[i - 1][pos] + (pref[j] - pref[pos]) * (pref[pos]) >= res){
res = dp[i - 1][pos] + (pref[j] - pref[pos]) * (pref[pos]);
p[i][j] = pos;
}
}
dp[i][j] = res;
}
}
cout << dp[k + 1][n] << '\n';
int x = k + 1, y = n;
while(x > 1){
y = p[x][y];
x--;
if(y != 0){
cout << y << ' ';
}
}
}
signed main(){
speed
int test = 1;
// cin >> test;
while(test--){
solve();
}
}
# | 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... |