이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast,unroll-loops")
#define ll long long
#define all(x) x.begin(), x.end()
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll INF = 1e9 + 7;
const int MAXN = 1e5 + 5;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ifstream cin("input.txt");
ofstream cout("output.txt");
int n, k;
cin >> n >> k;
vector<ll> a(n + 1);
for(int i = 1; i <= n; i++){
cin >> a[i];
}
vector<vector<ll> > dp(k + 2, vector<ll>(n + 2, -INF));
vector<vector<int> > p(k + 2, vector<int>(n + 2, 0));
dp[0][0] = 0;
vector<ll> pref(n + 2, 0);
for(int i = 1; i <= n; i++){
pref[i] += pref[i - 1];
pref[i] += a[i];
}
for(int i = 1; i <= k + 1; i++){
for(int j = 1; j <= n; j++){
for(int k = 0; k < j; k++){
if(dp[i][j] < dp[i - 1][k] + (pref[j] - pref[k]) * pref[k]){
dp[i][j] = dp[i - 1][k] + (pref[j] - pref[k]) * pref[k];
p[i][j] = k;
}
}
}
}
vector<int> ans;
int cur = n;
for(int i = k; i > 0; i--){
ans.push_back(p[i + 1][cur]);
cur = p[i + 1][cur];
}
cout << dp[k + 1][n] << endl;
reverse(all(ans));
for(auto i: ans){
cout << i << " ";
}
}
# | 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... |