//Convex Hull Trick, verified on CF319C (319 is ID)
import java.io.*; import java.util.*;
public class sequence{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
}
static ArrayList<Integer> M=new ArrayList<>();
static ArrayList<Long> B=new ArrayList<>();
static ArrayList<Integer> ind=new ArrayList<>();
static int pointer=0;
static short[][] ch; static boolean[][] b;
static int N;
public static void main(String[] args){
FastReader br=new FastReader();
N=br.nextInt(); int K=br.nextInt();
int[] s=new int[N];
long[] dp=new long[N];
long[] dp2=new long[N];
ch=new short[N][K]; b=new boolean[N][K];
s[0]=br.nextInt(); for(int i=1;i<N;i++){s[i]=s[i-1]+br.nextInt(); }
//System.out.println(Arrays.toString(s));
for (int i = 0; i < K; i++) {
M=new ArrayList<>();
B=new ArrayList<>();
ind=new ArrayList<>();
add(s[0],-(long)s[0]*s[0],i);
for (int j = 1; j < N; j++) {
dp2[j]=query((long)s[j],N*i+j);
add(s[j], dp[j]-(long)s[j]*s[j],Math.max(j,i));
//System.out.println(M);
//System.out.println(B);
}
for (int j = 0; j < N; j++) {
dp[j]=dp2[j];
}
//System.out.println(Arrays.toString(dp));
}
//System.out.println(Arrays.toString(dp));
System.out.println(dp[N-1]);
StringBuilder sb=new StringBuilder();
ArrayList<Integer> arl=new ArrayList<>();
int cur=N-1;
for (int i = K-1; i >=0; i--) {
if(b[cur][i])cur=(int) 2*ch[cur][i]+1;
else cur=(int) 2*ch[cur][i];
arl.add(cur);
}
//StringBuilder is actually quite memory consuming
for (int i = K-1; i >=0; i--) {
sb.append((arl.get(i)+1)+" ");
}
System.out.println(sb.toString());
}
public static boolean bad(int l1, int l2, int l3){//Using doubles to prevent overflow
return (M.get(l1)-M.get(l3))*((double)B.get(l2)-B.get(l1))>=(M.get(l1)-M.get(l2))*((double)B.get(l3)-B.get(l1));
//Intersection of l1 and l3 must be ABOVE l2
//For l2 to be added
//l1.m>l2.m>l3.m, as sum is strictly increasing
}
public static void add(int m, long b, int i){
M.add(m); B.add(b); ind.add(i);
while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)){
M.remove(M.size()-2); B.remove(B.size()-2); ind.remove(ind.size()-2);
}
}
public static long query(long x, int j){
if(pointer>=M.size()){pointer=M.size()-1;}
while(pointer<M.size()-1 &&
M.get(pointer+1)*x+B.get(pointer+1) > M.get(pointer)*x+B.get(pointer)){
pointer++;
}
ch[j%N][j/N]=(short)(ind.get(pointer)/2);
if(ind.get(pointer)%2==1){
b[j%N][j/N]=true;
}
return M.get(pointer)*x+B.get(pointer);
}
}
//Debugging:
//Are you sure your algorithm is correct?
//Bounds: long
//Special cases: n=0,1?
//Make sure you remove your debugging code before you submit!
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
111 ms |
9960 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
110 ms |
9580 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
112 ms |
9656 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
115 ms |
9444 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
111 ms |
9564 KB |
contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
111 ms |
9580 KB |
contestant found the optimal answer: 1 == 1 |
7 |
Correct |
115 ms |
9580 KB |
contestant found the optimal answer: 1 == 1 |
8 |
Correct |
110 ms |
9580 KB |
contestant found the optimal answer: 1 == 1 |
9 |
Correct |
110 ms |
9580 KB |
contestant found the optimal answer: 100400096 == 100400096 |
10 |
Correct |
112 ms |
9600 KB |
contestant found the optimal answer: 900320000 == 900320000 |
11 |
Correct |
108 ms |
9708 KB |
contestant found the optimal answer: 3698080248 == 3698080248 |
12 |
Correct |
113 ms |
9564 KB |
contestant found the optimal answer: 3200320000 == 3200320000 |
13 |
Correct |
108 ms |
9580 KB |
contestant found the optimal answer: 140072 == 140072 |
14 |
Correct |
107 ms |
9552 KB |
contestant found the optimal answer: 376041456 == 376041456 |
15 |
Correct |
108 ms |
9580 KB |
contestant found the optimal answer: 805 == 805 |
16 |
Correct |
114 ms |
9708 KB |
contestant found the optimal answer: 900189994 == 900189994 |
17 |
Correct |
107 ms |
9580 KB |
contestant found the optimal answer: 999919994 == 999919994 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
113 ms |
9580 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
123 ms |
9580 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
141 ms |
9840 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
115 ms |
9576 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
119 ms |
9836 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
115 ms |
9580 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
132 ms |
9708 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
118 ms |
9580 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
135 ms |
9580 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
126 ms |
9580 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
132 ms |
9708 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
124 ms |
9660 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
244 ms |
15148 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
128 ms |
9580 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
253 ms |
15788 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Correct |
254 ms |
15628 KB |
contestant found the optimal answer: 107630884 == 107630884 |
7 |
Correct |
248 ms |
15336 KB |
contestant found the optimal answer: 475357671774 == 475357671774 |
8 |
Correct |
188 ms |
12612 KB |
contestant found the optimal answer: 193556962 == 193556962 |
9 |
Correct |
169 ms |
10476 KB |
contestant found the optimal answer: 482389919803 == 482389919803 |
10 |
Correct |
230 ms |
15044 KB |
contestant found the optimal answer: 490686959791 == 490686959791 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
152 ms |
10112 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
151 ms |
9936 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
265 ms |
15624 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
154 ms |
10220 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
270 ms |
16636 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
274 ms |
16500 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
275 ms |
16272 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
280 ms |
16252 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
256 ms |
15704 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
264 ms |
15720 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
489 ms |
19680 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
488 ms |
19784 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
765 ms |
28332 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
467 ms |
17964 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
685 ms |
26056 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
723 ms |
25160 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
698 ms |
28768 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
639 ms |
26476 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
684 ms |
26036 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
756 ms |
26500 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
621 ms |
44164 KB |
Execution failed because the return code was nonzero |
2 |
Halted |
0 ms |
0 KB |
- |