이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "
const int MAX_N = 1e5 + 10, MAX_K = 210;
ll dp[MAX_N][MAX_K], hist[MAX_N][MAX_K];
ll pref[MAX_N], arr[MAX_N];
void output(int n, int k) {
if(k == 0) {
return;
}
output(hist[n][k], k - 1);
cout << hist[n][k] << " ";
}
signed main() {
//ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n, k;
cin >> n >> k;
for(int i = 1; i <= n; i ++) {
cin >> arr[i];
pref[i] = pref[i - 1] + arr[i];
}
for(int i = 0; i <= n; i ++) {
for(int j = 1; j <= k; j ++) {
dp[i][j] = -mod;
}
}
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= k; j ++) {
for(int q = 1; q < i; q ++) {
if(chkmax(dp[i][j], dp[q][j - 1] + pref[q] * pref[i] - pref[q] * pref[q])) {
hist[i][j] = q;
}
}
}
}
cout << dp[n][k] << endl;
output(n, k);
return 0;
}
/*
7 3
4 1 3 4 0 2 3
*/
# | 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... |