import java.io.*;
import java.util.*;
public class fancyfence
{
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
String next() { // reads in the next string
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() { // reads in the next int
return Integer.parseInt(next());
}
public long nextLong() { // reads in the next long
return Long.parseLong(next());
}
public double nextDouble() { // reads in the next double
return Double.parseDouble(next());
}
}
static long m = 1000000007;
static InputReader r = new InputReader(System.in);
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args)
{
int n = r.nextInt();
Stack<Integer> stack = new Stack<Integer>();
long[] h = new long[n+1]; long[] w = new long[n+1]; long[] total = new long[n+1]; long tot = 0;
for (int i = 1; i <= n; i ++)
{
h[i] = r.nextLong();
}
for (int i = 1; i <= n; i ++)
{
w[i] = r.nextLong();
}
long ans = 0;
stack.add(0);
for (int i = 1; i <= n; i ++)
{
long remove = 0;
while (h[stack.peek()] >= h[i])
{
int j = stack.pop();
remove += w[j]; remove%=m;
}
long x = ((h[i])*(h[i]+1))/2; x%=m; long y = ((w[i])*(w[i]+1))/2; y%=m;
tot = total[stack.peek()] + (remove*x)%m;
tot%=m;
ans += (tot*w[i])%m; ans%=m;
ans += (x*y)%m; ans%=m;
total[i] = tot + (w[i]*x)%m; total[i]%=m;
w[i] += remove; w[i]%=m;
stack.add(i);
}
pw.println(ans);
pw.close();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
8548 KB |
Output is correct |
2 |
Correct |
159 ms |
11668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
8364 KB |
Output is correct |
2 |
Correct |
79 ms |
8384 KB |
Output is correct |
3 |
Correct |
69 ms |
8424 KB |
Output is correct |
4 |
Correct |
71 ms |
8536 KB |
Output is correct |
5 |
Correct |
84 ms |
8640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
8480 KB |
Output is correct |
2 |
Correct |
119 ms |
9192 KB |
Output is correct |
3 |
Correct |
472 ms |
16724 KB |
Output is correct |
4 |
Correct |
471 ms |
20112 KB |
Output is correct |
5 |
Correct |
446 ms |
20240 KB |
Output is correct |
6 |
Correct |
507 ms |
20184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
166 ms |
11528 KB |
Output is correct |
2 |
Correct |
398 ms |
14908 KB |
Output is correct |
3 |
Correct |
426 ms |
18612 KB |
Output is correct |
4 |
Correct |
440 ms |
23748 KB |
Output is correct |
5 |
Correct |
463 ms |
25708 KB |
Output is correct |
6 |
Correct |
69 ms |
8584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
8324 KB |
Output is correct |
2 |
Correct |
167 ms |
11804 KB |
Output is correct |
3 |
Correct |
384 ms |
14868 KB |
Output is correct |
4 |
Correct |
419 ms |
18672 KB |
Output is correct |
5 |
Correct |
433 ms |
23720 KB |
Output is correct |
6 |
Correct |
459 ms |
25808 KB |
Output is correct |
7 |
Correct |
162 ms |
11764 KB |
Output is correct |
8 |
Correct |
386 ms |
14884 KB |
Output is correct |
9 |
Correct |
423 ms |
19208 KB |
Output is correct |
10 |
Correct |
439 ms |
23704 KB |
Output is correct |
11 |
Correct |
519 ms |
24564 KB |
Output is correct |
12 |
Correct |
72 ms |
8432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
8380 KB |
Output is correct |
2 |
Correct |
164 ms |
11440 KB |
Output is correct |
3 |
Correct |
84 ms |
8472 KB |
Output is correct |
4 |
Correct |
74 ms |
8456 KB |
Output is correct |
5 |
Correct |
68 ms |
8444 KB |
Output is correct |
6 |
Correct |
68 ms |
8456 KB |
Output is correct |
7 |
Correct |
69 ms |
8436 KB |
Output is correct |
8 |
Correct |
106 ms |
9384 KB |
Output is correct |
9 |
Correct |
211 ms |
11512 KB |
Output is correct |
10 |
Correct |
165 ms |
11712 KB |
Output is correct |
11 |
Correct |
72 ms |
8544 KB |
Output is correct |
12 |
Correct |
87 ms |
8540 KB |
Output is correct |
13 |
Correct |
183 ms |
11656 KB |
Output is correct |
14 |
Correct |
180 ms |
11852 KB |
Output is correct |
15 |
Correct |
182 ms |
11580 KB |
Output is correct |
16 |
Correct |
70 ms |
8520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
8548 KB |
Output is correct |
2 |
Correct |
159 ms |
11668 KB |
Output is correct |
3 |
Correct |
72 ms |
8540 KB |
Output is correct |
4 |
Correct |
163 ms |
11740 KB |
Output is correct |
5 |
Correct |
67 ms |
8288 KB |
Output is correct |
6 |
Correct |
72 ms |
8200 KB |
Output is correct |
7 |
Correct |
74 ms |
8404 KB |
Output is correct |
8 |
Correct |
79 ms |
8204 KB |
Output is correct |
9 |
Correct |
67 ms |
8600 KB |
Output is correct |
10 |
Correct |
104 ms |
9328 KB |
Output is correct |
11 |
Correct |
454 ms |
16844 KB |
Output is correct |
12 |
Correct |
460 ms |
20032 KB |
Output is correct |
13 |
Correct |
490 ms |
20160 KB |
Output is correct |
14 |
Correct |
450 ms |
20180 KB |
Output is correct |
15 |
Correct |
178 ms |
11748 KB |
Output is correct |
16 |
Correct |
391 ms |
14804 KB |
Output is correct |
17 |
Correct |
461 ms |
18444 KB |
Output is correct |
18 |
Correct |
448 ms |
23796 KB |
Output is correct |
19 |
Correct |
445 ms |
25676 KB |
Output is correct |
20 |
Correct |
181 ms |
11588 KB |
Output is correct |
21 |
Correct |
354 ms |
14932 KB |
Output is correct |
22 |
Correct |
429 ms |
19376 KB |
Output is correct |
23 |
Correct |
436 ms |
23752 KB |
Output is correct |
24 |
Correct |
457 ms |
24668 KB |
Output is correct |
25 |
Correct |
94 ms |
8248 KB |
Output is correct |
26 |
Correct |
89 ms |
8508 KB |
Output is correct |
27 |
Correct |
160 ms |
11616 KB |
Output is correct |
28 |
Correct |
174 ms |
11672 KB |
Output is correct |
29 |
Correct |
180 ms |
11516 KB |
Output is correct |
30 |
Correct |
373 ms |
14780 KB |
Output is correct |
31 |
Correct |
368 ms |
14876 KB |
Output is correct |
32 |
Correct |
420 ms |
17608 KB |
Output is correct |
33 |
Correct |
425 ms |
17556 KB |
Output is correct |
34 |
Correct |
476 ms |
23604 KB |
Output is correct |
35 |
Correct |
440 ms |
23576 KB |
Output is correct |
36 |
Correct |
480 ms |
24468 KB |
Output is correct |
37 |
Correct |
475 ms |
23916 KB |
Output is correct |
38 |
Correct |
62 ms |
8360 KB |
Output is correct |
39 |
Correct |
444 ms |
23940 KB |
Output is correct |
40 |
Correct |
436 ms |
23828 KB |
Output is correct |
41 |
Correct |
462 ms |
23944 KB |
Output is correct |
42 |
Correct |
472 ms |
24240 KB |
Output is correct |