이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <bitset>
#include <math.h>
#include <fstream>
#include <iomanip>
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vvvvll = vector<vvvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vstr = vector<string>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
using pb = pair<bool, bool>;
using vpb = vector<pb>;
using vvpb = vector<vpb>;
using vi = vector<int>;
using vvi = vector<vi>;
const ll mod = (ll)1e9 + 7;
const ll inf = (ll)1e18;
#define FAST ios_base::sync_with_stdio(0)
#define FASTIN cin.tie(0)
#define FASTOUT cout.tie(0)
#define upmin(a, b) a = min(a, b)
#define upmax(a, b) a = max(a, b)
#define pr(x) cout << x << endl;
#define prv(v) for(auto it = v.begin(); it != v.end(); ++it) cout << *it << " "; cout << endl;
#define prvv(v) for(auto it : v) { for(auto it2 : it) cout << it2 << " "; cout << endl; } cout << endl;
#define wpr(x) cout << #x << " = " << (x) << endl;
#define wprv(v) cout << #v << ": " << endl; for(auto it : v) cout << *it << " "; cout << endl;
#define wprvv(v) cout << #v << ": " << endl; for(auto it : v) { for(auto it2 : it) cout << it2 << " "; cout << endl; } cout << endl;
#define rep(i,s,e) for(ll i = s; i < e; i++)
#define all(x) x.begin(),x.end()
#define pb push_back
ll n, k;
vll arr;
vvll dp, last;
vll p;
/*
dp[layer][i] = max sum using layer seperators and i is the last one (and it is AFTER i)
*/
void solve()
{
cin >> n >> k;
arr.resize(n);
rep(i, 0, n) cin >> arr[i];
p.resize(n);
p[0] = arr[0];
rep(i, 1, n) p[i] = arr[i] + p[i - 1];
dp.resize(k + 1, vll(n + 1, 0));
last.resize(k + 1, vll(n + 1, -1));
rep(l, 1, k + 1) {
rep(i, 0, n) {
rep(j, 0, i - 1) {
ll v = dp[l - 1][j] + (p[i] - p[j])*(p[n-1] - p[i]);
if (dp[l][i] < v) {
dp[l][i] = v;
last[l][i] = j;
}
}
ll vs = p[i] * (p[n - 1] - p[i]);
if (dp[l][i] < vs) {
dp[l][i] = vs;
last[l][i] = -1;
}
}
}
//wprvv(dp);
ll ans = -1;
ll mxind = -1;
rep(i, 0, n) {
if (dp[k][i] > ans) {
ans = dp[k][i];
mxind = i;
}
}
rep(l, 0, k+1) {
rep(i, 0, n) {
if (dp[l][i] > ans) {
ans = dp[l][i];
mxind = i;
}
}
}
vll used;
ll cur = mxind;
ll left = k;
while (cur != -1) {
used.push_back(cur+1);
cur = last[left][cur];
left--;
}
reverse(used.begin(), used.end());
//used.pop_back();
pr(ans);
prv(used);
}
int main()
{
FAST;
FASTIN; FASTOUT;
solve();
}
/*
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... |