import java.util.*;
import java.io.*;
public class Discharging {
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public int ni() throws IOException {
int ret = 0;
boolean flip = false;
byte c = read();
while (c < '0' || c > '9') {
if (c == '-') flip = true;
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
return flip ? -ret : ret;
}
public long nl() throws IOException {
long ret = 0;
boolean flip = false;
byte c = read();
while (c < '0' || c > '9') {
if (c == '-') flip = true;
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
return flip ? -ret : ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) {
buffer[0] = -1;
}
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead) {
fillBuffer();
}
return buffer[bufferPointer++];
}
}
public static void main(String[] args) throws IOException {
Reader sc = new Reader();
PrintStream out = new PrintStream(new BufferedOutputStream(System.out));
int N = sc.ni();
long[] nums = new long[N+1];
for (int i = 1; i <= N; i++) {
nums[i] = Math.max(nums[i-1],sc.nl());
}
//dp with convex hull trick
long[] dp = new long[N+1];
Hull cht = new Hull();
for (int i = 1; i <= N; i++) {
cht.add(new Line(nums[N-(i-1)],dp[i-1]));
dp[i] = cht.getMin(i);
}
out.print(dp[N]);
out.flush(); // remember to flush just once, at the very end of your program
}
//convex hull
static class Hull {
ArrayDeque<Line> ad;
public Hull() {
ad = new ArrayDeque<Line>();
}
public void add(Line l) {
if (ad.isEmpty() || l.m < ad.peekLast().m) {
while (ad.size() >= 2) {
Line l1 = ad.pollLast();
Line l2 = ad.peekLast();
if (good(l2,l1,l)) {
ad.addLast(l1);
break;
}
}
ad.addLast(l);
}
}
public boolean good(Line l1, Line l2, Line l3) {
double d12 = (l2.b-l1.b+0.0)/(l1.m-l2.m);
double d23 = (l3.b-l2.b+0.0)/(l2.m-l3.m);
return (d12 < d23);
}
public long getMin(long x) {
while (ad.size() >= 2) {
Line f1 = ad.pollFirst();
Line f2 = ad.peekFirst();
if ((f2.b-f1.b) > x*(f1.m-f2.m)) {
ad.addFirst(f1);
break;
}
}
return ad.peekFirst().eval(x);
}
}
//line
static class Line {
long m;
long b;
public Line(long m, long b) {
this.m=m;
this.b=b;
}
public long eval(long x) {
return m*x+b;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
8808 KB |
Output is correct |
2 |
Correct |
72 ms |
8556 KB |
Output is correct |
3 |
Correct |
71 ms |
8556 KB |
Output is correct |
4 |
Correct |
72 ms |
8556 KB |
Output is correct |
5 |
Correct |
72 ms |
8556 KB |
Output is correct |
6 |
Correct |
72 ms |
8556 KB |
Output is correct |
7 |
Correct |
76 ms |
8556 KB |
Output is correct |
8 |
Correct |
75 ms |
8624 KB |
Output is correct |
9 |
Correct |
76 ms |
8540 KB |
Output is correct |
10 |
Correct |
74 ms |
8556 KB |
Output is correct |
11 |
Correct |
75 ms |
8556 KB |
Output is correct |
12 |
Correct |
74 ms |
8556 KB |
Output is correct |
13 |
Correct |
74 ms |
8556 KB |
Output is correct |
14 |
Correct |
73 ms |
8556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
8684 KB |
Output is correct |
2 |
Correct |
92 ms |
8668 KB |
Output is correct |
3 |
Correct |
88 ms |
8560 KB |
Output is correct |
4 |
Correct |
81 ms |
8684 KB |
Output is correct |
5 |
Correct |
86 ms |
8552 KB |
Output is correct |
6 |
Correct |
93 ms |
8556 KB |
Output is correct |
7 |
Correct |
83 ms |
8744 KB |
Output is correct |
8 |
Correct |
89 ms |
8812 KB |
Output is correct |
9 |
Correct |
89 ms |
8812 KB |
Output is correct |
10 |
Correct |
90 ms |
8784 KB |
Output is correct |
11 |
Correct |
94 ms |
8656 KB |
Output is correct |
12 |
Correct |
85 ms |
8636 KB |
Output is correct |
13 |
Correct |
83 ms |
8556 KB |
Output is correct |
14 |
Correct |
84 ms |
8556 KB |
Output is correct |
15 |
Correct |
84 ms |
8572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
8684 KB |
Output is correct |
2 |
Correct |
92 ms |
8668 KB |
Output is correct |
3 |
Correct |
88 ms |
8560 KB |
Output is correct |
4 |
Correct |
81 ms |
8684 KB |
Output is correct |
5 |
Correct |
86 ms |
8552 KB |
Output is correct |
6 |
Correct |
93 ms |
8556 KB |
Output is correct |
7 |
Correct |
83 ms |
8744 KB |
Output is correct |
8 |
Correct |
89 ms |
8812 KB |
Output is correct |
9 |
Correct |
89 ms |
8812 KB |
Output is correct |
10 |
Correct |
90 ms |
8784 KB |
Output is correct |
11 |
Correct |
94 ms |
8656 KB |
Output is correct |
12 |
Correct |
85 ms |
8636 KB |
Output is correct |
13 |
Correct |
83 ms |
8556 KB |
Output is correct |
14 |
Correct |
84 ms |
8556 KB |
Output is correct |
15 |
Correct |
84 ms |
8572 KB |
Output is correct |
16 |
Correct |
302 ms |
31556 KB |
Output is correct |
17 |
Correct |
339 ms |
34268 KB |
Output is correct |
18 |
Correct |
333 ms |
35088 KB |
Output is correct |
19 |
Correct |
343 ms |
35148 KB |
Output is correct |
20 |
Correct |
315 ms |
33640 KB |
Output is correct |
21 |
Correct |
312 ms |
33796 KB |
Output is correct |
22 |
Correct |
337 ms |
35240 KB |
Output is correct |
23 |
Correct |
304 ms |
33700 KB |
Output is correct |
24 |
Correct |
316 ms |
33416 KB |
Output is correct |
25 |
Correct |
324 ms |
33564 KB |
Output is correct |
26 |
Correct |
310 ms |
33472 KB |
Output is correct |
27 |
Correct |
308 ms |
33508 KB |
Output is correct |
28 |
Correct |
328 ms |
33724 KB |
Output is correct |
29 |
Correct |
322 ms |
33708 KB |
Output is correct |
30 |
Correct |
310 ms |
33712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
180 ms |
26732 KB |
Output is correct |
2 |
Correct |
184 ms |
27380 KB |
Output is correct |
3 |
Correct |
184 ms |
27500 KB |
Output is correct |
4 |
Correct |
188 ms |
27692 KB |
Output is correct |
5 |
Correct |
181 ms |
27344 KB |
Output is correct |
6 |
Correct |
180 ms |
27388 KB |
Output is correct |
7 |
Correct |
180 ms |
27360 KB |
Output is correct |
8 |
Correct |
183 ms |
27252 KB |
Output is correct |
9 |
Correct |
180 ms |
27380 KB |
Output is correct |
10 |
Correct |
179 ms |
27484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
8808 KB |
Output is correct |
2 |
Correct |
72 ms |
8556 KB |
Output is correct |
3 |
Correct |
71 ms |
8556 KB |
Output is correct |
4 |
Correct |
72 ms |
8556 KB |
Output is correct |
5 |
Correct |
72 ms |
8556 KB |
Output is correct |
6 |
Correct |
72 ms |
8556 KB |
Output is correct |
7 |
Correct |
76 ms |
8556 KB |
Output is correct |
8 |
Correct |
75 ms |
8624 KB |
Output is correct |
9 |
Correct |
76 ms |
8540 KB |
Output is correct |
10 |
Correct |
74 ms |
8556 KB |
Output is correct |
11 |
Correct |
75 ms |
8556 KB |
Output is correct |
12 |
Correct |
74 ms |
8556 KB |
Output is correct |
13 |
Correct |
74 ms |
8556 KB |
Output is correct |
14 |
Correct |
73 ms |
8556 KB |
Output is correct |
15 |
Correct |
91 ms |
8684 KB |
Output is correct |
16 |
Correct |
92 ms |
8668 KB |
Output is correct |
17 |
Correct |
88 ms |
8560 KB |
Output is correct |
18 |
Correct |
81 ms |
8684 KB |
Output is correct |
19 |
Correct |
86 ms |
8552 KB |
Output is correct |
20 |
Correct |
93 ms |
8556 KB |
Output is correct |
21 |
Correct |
83 ms |
8744 KB |
Output is correct |
22 |
Correct |
89 ms |
8812 KB |
Output is correct |
23 |
Correct |
89 ms |
8812 KB |
Output is correct |
24 |
Correct |
90 ms |
8784 KB |
Output is correct |
25 |
Correct |
94 ms |
8656 KB |
Output is correct |
26 |
Correct |
85 ms |
8636 KB |
Output is correct |
27 |
Correct |
83 ms |
8556 KB |
Output is correct |
28 |
Correct |
84 ms |
8556 KB |
Output is correct |
29 |
Correct |
84 ms |
8572 KB |
Output is correct |
30 |
Correct |
77 ms |
8720 KB |
Output is correct |
31 |
Correct |
80 ms |
8444 KB |
Output is correct |
32 |
Correct |
78 ms |
8684 KB |
Output is correct |
33 |
Correct |
79 ms |
8656 KB |
Output is correct |
34 |
Correct |
78 ms |
8668 KB |
Output is correct |
35 |
Correct |
79 ms |
8668 KB |
Output is correct |
36 |
Correct |
80 ms |
8684 KB |
Output is correct |
37 |
Correct |
79 ms |
8700 KB |
Output is correct |
38 |
Correct |
78 ms |
8684 KB |
Output is correct |
39 |
Correct |
79 ms |
8540 KB |
Output is correct |
40 |
Correct |
79 ms |
8680 KB |
Output is correct |
41 |
Correct |
78 ms |
8744 KB |
Output is correct |
42 |
Correct |
78 ms |
8556 KB |
Output is correct |
43 |
Correct |
83 ms |
8684 KB |
Output is correct |
44 |
Correct |
77 ms |
8684 KB |
Output is correct |
45 |
Correct |
79 ms |
8684 KB |
Output is correct |
46 |
Correct |
78 ms |
8684 KB |
Output is correct |
47 |
Correct |
77 ms |
8668 KB |
Output is correct |
48 |
Correct |
78 ms |
8684 KB |
Output is correct |
49 |
Correct |
84 ms |
8684 KB |
Output is correct |
50 |
Correct |
78 ms |
8660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
8808 KB |
Output is correct |
2 |
Correct |
72 ms |
8556 KB |
Output is correct |
3 |
Correct |
71 ms |
8556 KB |
Output is correct |
4 |
Correct |
72 ms |
8556 KB |
Output is correct |
5 |
Correct |
72 ms |
8556 KB |
Output is correct |
6 |
Correct |
72 ms |
8556 KB |
Output is correct |
7 |
Correct |
76 ms |
8556 KB |
Output is correct |
8 |
Correct |
75 ms |
8624 KB |
Output is correct |
9 |
Correct |
76 ms |
8540 KB |
Output is correct |
10 |
Correct |
74 ms |
8556 KB |
Output is correct |
11 |
Correct |
75 ms |
8556 KB |
Output is correct |
12 |
Correct |
74 ms |
8556 KB |
Output is correct |
13 |
Correct |
74 ms |
8556 KB |
Output is correct |
14 |
Correct |
73 ms |
8556 KB |
Output is correct |
15 |
Correct |
91 ms |
8684 KB |
Output is correct |
16 |
Correct |
92 ms |
8668 KB |
Output is correct |
17 |
Correct |
88 ms |
8560 KB |
Output is correct |
18 |
Correct |
81 ms |
8684 KB |
Output is correct |
19 |
Correct |
86 ms |
8552 KB |
Output is correct |
20 |
Correct |
93 ms |
8556 KB |
Output is correct |
21 |
Correct |
83 ms |
8744 KB |
Output is correct |
22 |
Correct |
89 ms |
8812 KB |
Output is correct |
23 |
Correct |
89 ms |
8812 KB |
Output is correct |
24 |
Correct |
90 ms |
8784 KB |
Output is correct |
25 |
Correct |
94 ms |
8656 KB |
Output is correct |
26 |
Correct |
85 ms |
8636 KB |
Output is correct |
27 |
Correct |
83 ms |
8556 KB |
Output is correct |
28 |
Correct |
84 ms |
8556 KB |
Output is correct |
29 |
Correct |
84 ms |
8572 KB |
Output is correct |
30 |
Correct |
302 ms |
31556 KB |
Output is correct |
31 |
Correct |
339 ms |
34268 KB |
Output is correct |
32 |
Correct |
333 ms |
35088 KB |
Output is correct |
33 |
Correct |
343 ms |
35148 KB |
Output is correct |
34 |
Correct |
315 ms |
33640 KB |
Output is correct |
35 |
Correct |
312 ms |
33796 KB |
Output is correct |
36 |
Correct |
337 ms |
35240 KB |
Output is correct |
37 |
Correct |
304 ms |
33700 KB |
Output is correct |
38 |
Correct |
316 ms |
33416 KB |
Output is correct |
39 |
Correct |
324 ms |
33564 KB |
Output is correct |
40 |
Correct |
310 ms |
33472 KB |
Output is correct |
41 |
Correct |
308 ms |
33508 KB |
Output is correct |
42 |
Correct |
328 ms |
33724 KB |
Output is correct |
43 |
Correct |
322 ms |
33708 KB |
Output is correct |
44 |
Correct |
310 ms |
33712 KB |
Output is correct |
45 |
Correct |
180 ms |
26732 KB |
Output is correct |
46 |
Correct |
184 ms |
27380 KB |
Output is correct |
47 |
Correct |
184 ms |
27500 KB |
Output is correct |
48 |
Correct |
188 ms |
27692 KB |
Output is correct |
49 |
Correct |
181 ms |
27344 KB |
Output is correct |
50 |
Correct |
180 ms |
27388 KB |
Output is correct |
51 |
Correct |
180 ms |
27360 KB |
Output is correct |
52 |
Correct |
183 ms |
27252 KB |
Output is correct |
53 |
Correct |
180 ms |
27380 KB |
Output is correct |
54 |
Correct |
179 ms |
27484 KB |
Output is correct |
55 |
Correct |
77 ms |
8720 KB |
Output is correct |
56 |
Correct |
80 ms |
8444 KB |
Output is correct |
57 |
Correct |
78 ms |
8684 KB |
Output is correct |
58 |
Correct |
79 ms |
8656 KB |
Output is correct |
59 |
Correct |
78 ms |
8668 KB |
Output is correct |
60 |
Correct |
79 ms |
8668 KB |
Output is correct |
61 |
Correct |
80 ms |
8684 KB |
Output is correct |
62 |
Correct |
79 ms |
8700 KB |
Output is correct |
63 |
Correct |
78 ms |
8684 KB |
Output is correct |
64 |
Correct |
79 ms |
8540 KB |
Output is correct |
65 |
Correct |
79 ms |
8680 KB |
Output is correct |
66 |
Correct |
78 ms |
8744 KB |
Output is correct |
67 |
Correct |
78 ms |
8556 KB |
Output is correct |
68 |
Correct |
83 ms |
8684 KB |
Output is correct |
69 |
Correct |
77 ms |
8684 KB |
Output is correct |
70 |
Correct |
79 ms |
8684 KB |
Output is correct |
71 |
Correct |
78 ms |
8684 KB |
Output is correct |
72 |
Correct |
77 ms |
8668 KB |
Output is correct |
73 |
Correct |
78 ms |
8684 KB |
Output is correct |
74 |
Correct |
84 ms |
8684 KB |
Output is correct |
75 |
Correct |
78 ms |
8660 KB |
Output is correct |
76 |
Correct |
245 ms |
35248 KB |
Output is correct |
77 |
Correct |
258 ms |
35292 KB |
Output is correct |
78 |
Correct |
249 ms |
35240 KB |
Output is correct |
79 |
Correct |
250 ms |
35304 KB |
Output is correct |
80 |
Correct |
251 ms |
35160 KB |
Output is correct |
81 |
Correct |
251 ms |
35272 KB |
Output is correct |
82 |
Correct |
252 ms |
35432 KB |
Output is correct |
83 |
Correct |
243 ms |
35340 KB |
Output is correct |
84 |
Correct |
260 ms |
35332 KB |
Output is correct |
85 |
Correct |
248 ms |
35340 KB |
Output is correct |
86 |
Correct |
242 ms |
35256 KB |
Output is correct |
87 |
Correct |
248 ms |
35304 KB |
Output is correct |
88 |
Correct |
246 ms |
35420 KB |
Output is correct |
89 |
Correct |
246 ms |
35300 KB |
Output is correct |
90 |
Correct |
239 ms |
35416 KB |
Output is correct |
91 |
Correct |
245 ms |
35304 KB |
Output is correct |
92 |
Correct |
332 ms |
34724 KB |
Output is correct |
93 |
Correct |
314 ms |
33880 KB |
Output is correct |
94 |
Correct |
244 ms |
35288 KB |
Output is correct |
95 |
Correct |
250 ms |
35304 KB |
Output is correct |
96 |
Correct |
248 ms |
35232 KB |
Output is correct |
97 |
Correct |
245 ms |
35304 KB |
Output is correct |
98 |
Correct |
240 ms |
35288 KB |
Output is correct |
99 |
Correct |
242 ms |
35492 KB |
Output is correct |
100 |
Correct |
248 ms |
35384 KB |
Output is correct |