n = int(input())
a = [0] * n
b = [0] * n
for i in range(n):
x, y = map(int, input().split())
a[i] = x
b[i] = y
# subtask 1 only
ans = 0
extra = 0
suffix = []
for i in range(n):
ans += abs(extra)
extra += a[i] - b[i]
suffix.append(-1 if extra > 0 else 1)
if extra == 1:
# subtask 2
current = ans
suffix.pop()
total = 0
while len(suffix) > 0:
total += suffix.pop()
ans = min(ans, current + total)
print(ans)
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
2908 KB |
Output is correct |
2 |
Correct |
15 ms |
2908 KB |
Output is correct |
3 |
Correct |
18 ms |
3116 KB |
Output is correct |
4 |
Correct |
93 ms |
5768 KB |
Output is correct |
5 |
Correct |
180 ms |
9000 KB |
Output is correct |
6 |
Correct |
444 ms |
27872 KB |
Output is correct |
7 |
Correct |
842 ms |
52020 KB |
Output is correct |
8 |
Correct |
833 ms |
34812 KB |
Output is correct |
9 |
Correct |
842 ms |
34048 KB |
Output is correct |
10 |
Correct |
752 ms |
16396 KB |
Output is correct |
11 |
Correct |
761 ms |
16448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
2908 KB |
Output is correct |
2 |
Correct |
15 ms |
2908 KB |
Output is correct |
3 |
Correct |
18 ms |
3116 KB |
Output is correct |
4 |
Correct |
93 ms |
5768 KB |
Output is correct |
5 |
Correct |
180 ms |
9000 KB |
Output is correct |
6 |
Correct |
444 ms |
27872 KB |
Output is correct |
7 |
Correct |
842 ms |
52020 KB |
Output is correct |
8 |
Correct |
833 ms |
34812 KB |
Output is correct |
9 |
Correct |
842 ms |
34048 KB |
Output is correct |
10 |
Correct |
752 ms |
16396 KB |
Output is correct |
11 |
Correct |
761 ms |
16448 KB |
Output is correct |
12 |
Correct |
298 ms |
15248 KB |
Output is correct |
13 |
Correct |
609 ms |
32412 KB |
Output is correct |
14 |
Execution timed out |
1044 ms |
51836 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
2908 KB |
Output is correct |
2 |
Correct |
15 ms |
2908 KB |
Output is correct |
3 |
Incorrect |
13 ms |
2908 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
2908 KB |
Output is correct |
2 |
Correct |
15 ms |
2908 KB |
Output is correct |
3 |
Correct |
18 ms |
3116 KB |
Output is correct |
4 |
Incorrect |
13 ms |
2908 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
2908 KB |
Output is correct |
2 |
Correct |
15 ms |
2908 KB |
Output is correct |
3 |
Correct |
18 ms |
3116 KB |
Output is correct |
4 |
Incorrect |
13 ms |
2908 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |