#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end
class CHT {
struct F{
ll a,b; int idx;
F(ll a,ll b,int idx):a(a),b(b),idx(idx){}
};
deque<F> deq;
bool check(F &f1,F &f2,F &f3) {
return (f2.a - f1.a) * (f3.b - f2.b) >= (f2.b - f1.b) * (f3.a - f2.a);
}
ll f(F &f1,ll x) {return f1.a * x + f1.b;}
public:
void add_line(ll a,ll b,int idx) {
F f1 = F(a,b,idx);
while(deq.size() >= 2 and check(deq[deq.size()-2], deq[deq.size()-1], f1)) {
deq.pop_back();
} deq.push_back(f1);
}
pair<ll,int> query(ll x) {
while(deq.size() >= 2 and f(deq[0], x) <= f(deq[1], x)) {
deq.pop_front();
} return {f(deq[0], x),deq[0].idx};
}
};
ll dp[210][101010]={},pre[210][101010];
int main(){
int n,k; cin>>n>>k;
vector<ll> a(n),rui(n+1);
rep(i,0,n){
cin>>a[i]; rui[i+1]=a[i];
}
rep(i,0,n)rui[i+1]+=rui[i];
rep(i,0,k+1)rep(j,0,n+1)dp[i][j]=-INF;
dp[0][0]=0;
/*
rep(i,1,k+1)rep(j,1,n)rep(i2,0,j)if(dp[i-1][i2]>=0){
ll add=(rui[j]-rui[i2])*(rui[n]-rui[j]);
if(chmax(dp[i][j],dp[i-1][i2]+add))pre[i][j]=i2;
}
*/
rep(i,1,k+1){
CHT cht;
rep(j,1,n){
if(dp[i-1][j-1]>=0)cht.add_line(-rui[j-1],dp[i-1][j-1],j-1);
auto [v,x]=cht.query(rui[n]-rui[j]);
if(chmax(dp[i][j],v+rui[j]*(rui[n]-rui[j])))pre[i][j]=x;
}
}
ll res=-INF; int pos=-1;
rep(i,0,n)if(chmax(res,dp[k][i]))pos=i;
cout<<res<<endl;
int cur=k; vector<int> ps;
while(cur){
ps.push_back(pos);
pos=pre[cur][pos]; cur--;
}
reverse(ALL(ps));
rep(i,0,k)cout<<ps[i]<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 1 == 1 |
7 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 1 == 1 |
8 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 1 == 1 |
9 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 100400096 == 100400096 |
10 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 900320000 == 900320000 |
11 |
Incorrect |
0 ms |
384 KB |
contestant didn't find the optimal answer: 3498210227 < 3698080248 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
384 KB |
contestant didn't find the optimal answer: 1091288 < 1093956 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Incorrect |
3 ms |
3200 KB |
Integer 0 violates the range [1, 199] |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
contestant didn't find the optimal answer: 20180056 < 21503404 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
1024 KB |
contestant didn't find the optimal answer: 1794250000 < 1818678304 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
38 ms |
7436 KB |
contestant didn't find the optimal answer: 13199022576 < 19795776960 |
2 |
Halted |
0 ms |
0 KB |
- |