#include <bits/stdc++.h>
#define MAX 105000
typedef long long ftype;
typedef std::pair<ftype,ftype> point;
ftype f(point a, ftype x) {
return a.first*x + a.second;
}
const long long val = 1LL<<30LL;
const int maxn = 4e5;
const int membros = 25 * maxn;
point mem[membros];
int lefta[membros],righta[membros];
int cur=3;
int alocar(void){
int x=cur;
if(x==20*maxn)assert(0);
cur++;
mem[x]={0,-1LL<<62LL};
lefta[x]=righta[x]=0;
return x;
}
int inicio = alocar();
void clear(void){
cur=3;
inicio=alocar();
}
void add_line(point nw, int v = inicio, long long l = -val, long long r = val) {
long long m = (l + r) / 2LL;
bool lef = f(nw, l) > f(mem[v], l);
bool mid = f(nw, m) > f(mem[v], m);
if(mid) {
std::swap(mem[v], nw);
}
if(r-l==1) {
return;
} else if(lef != mid) {
if(!lefta[v])lefta[v]=alocar();
add_line(nw, lefta[v], l, m);
} else {
if(!righta[v])righta[v]=alocar();
add_line(nw,righta[v], m, r);
}
}
typedef std::pair<long long,point> plpoi;
plpoi get(int x, int v = inicio, long long l = -val, long long r = val) {
if(!v)return {-(1LL<<62LL),{-1,-1}};
long long m = (l + r) / 2LL;
if(r-l==1) {
return {f(mem[v], x),mem[v]};
} else if(x < m) {
return std::max({f(mem[v], x),mem[v]}, get(x, lefta[v], l, m));
} else {
return std::max({f(mem[v], x),mem[v]}, get(x, righta[v], m, r));
}
}
long long array[MAX];
long long pref[MAX];
point prevans[MAX];
int backtracking[MAX][205];
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
for(auto&x:prevans)x={0,-1LL<<62LL};
int N,K;
std::cin>>N>>K;
for(int i=0;i!=N;++i){
std::cin>>array[i];
if(i){
pref[i]=pref[i-1];
}
pref[i]+=array[i];
}
pref[N]=1LL<<50LL;
long long resp=0;
for(int i=0;i!=K+1;++i){
clear();
add_line({0,0});
std::map<point,int> mapa;
for(int j=0;j!=N;++j){
if(j!=N-1){
add_line(prevans[j]);
mapa[prevans[j]]=j;}
long long tot = pref[j];
long long ans = 0;
if(j){
auto a=get(pref[j]);
ans=a.first;
backtracking[j][i]=mapa[a.second];
}
resp=ans;
prevans[j]={tot,-(pref[j]*tot)+ans};
}
}
std::vector<int> bck;
int cur=N-1;
for(int i=K;i!=0;--i){
int t = backtracking[cur][i];
bck.push_back(t+1);
cur=t;
}
std::reverse(bck.begin(),bck.end());
std::cout<<resp<<"\n";
for(int i=0;i!=bck.size()-1;++i)std::cout<<bck[i]<<" ";
std::cout<<bck.back()<<"\n";
}
Compilation message
sequence.cpp: In function 'int main()':
sequence.cpp:106:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for(int i=0;i!=bck.size()-1;++i)std::cout<<bck[i]<<" ";
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2004 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
1 ms |
2004 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
1 ms |
2004 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
2 ms |
2004 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
1 ms |
2004 KB |
contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
2 ms |
2008 KB |
contestant found the optimal answer: 1 == 1 |
7 |
Correct |
1 ms |
2004 KB |
contestant found the optimal answer: 1 == 1 |
8 |
Correct |
1 ms |
2004 KB |
contestant found the optimal answer: 1 == 1 |
9 |
Correct |
2 ms |
2004 KB |
contestant found the optimal answer: 100400096 == 100400096 |
10 |
Correct |
1 ms |
2004 KB |
contestant found the optimal answer: 900320000 == 900320000 |
11 |
Correct |
1 ms |
2004 KB |
contestant found the optimal answer: 3698080248 == 3698080248 |
12 |
Correct |
1 ms |
2004 KB |
contestant found the optimal answer: 3200320000 == 3200320000 |
13 |
Correct |
1 ms |
2012 KB |
contestant found the optimal answer: 140072 == 140072 |
14 |
Correct |
1 ms |
2004 KB |
contestant found the optimal answer: 376041456 == 376041456 |
15 |
Correct |
1 ms |
2012 KB |
contestant found the optimal answer: 805 == 805 |
16 |
Correct |
1 ms |
2004 KB |
contestant found the optimal answer: 900189994 == 900189994 |
17 |
Correct |
1 ms |
2004 KB |
contestant found the optimal answer: 999919994 == 999919994 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2004 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
2 ms |
2004 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
2 ms |
2004 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
1 ms |
2004 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
2 ms |
2004 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
1 ms |
2004 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
2 ms |
2004 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
2 ms |
2004 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
1 ms |
2004 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
2 ms |
2004 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2132 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
2 ms |
2132 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
20 ms |
2220 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
2 ms |
2132 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
16 ms |
2132 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Incorrect |
21 ms |
2196 KB |
position 199 occurs twice in split scheme |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2896 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
3 ms |
2900 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
93 ms |
3080 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
4 ms |
3028 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
112 ms |
2912 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
96 ms |
2924 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
91 ms |
3072 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
105 ms |
3032 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
19 ms |
3068 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
41 ms |
3148 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
11092 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
19 ms |
11092 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
1113 ms |
12828 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
26 ms |
12756 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
748 ms |
11168 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
812 ms |
11240 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
820 ms |
12688 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
708 ms |
12240 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
737 ms |
12532 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
1041 ms |
12500 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
255 ms |
93692 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
255 ms |
93700 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Execution timed out |
2083 ms |
109176 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |