#include <cstdlib>
#include <iostream>
#include <vector>
int main() {
std::cin.tie(NULL)->sync_with_stdio(false);
int n;
std::cin >> n;
std::vector<int> diff(n);
long long ans = 0;
long long total = 0;
for (int i = 0; i < n; i++) {
int a, b;
std::cin >> a >> b;
total += a - b;
diff[i] = total > 0 ? -1 : 1;
ans += std::abs(total);
}
if (total == 1) {
long long current = ans, total_diff = 0;
while (!diff.empty()) {
total_diff += diff.back();
diff.pop_back();
ans = std::min(ans, current + total_diff);
}
}
std::cout << ans << std::endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
604 KB |
Output is correct |
5 |
Correct |
8 ms |
856 KB |
Output is correct |
6 |
Correct |
27 ms |
1372 KB |
Output is correct |
7 |
Correct |
51 ms |
2396 KB |
Output is correct |
8 |
Correct |
46 ms |
7124 KB |
Output is correct |
9 |
Correct |
46 ms |
6484 KB |
Output is correct |
10 |
Correct |
32 ms |
4200 KB |
Output is correct |
11 |
Correct |
32 ms |
4188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
604 KB |
Output is correct |
5 |
Correct |
8 ms |
856 KB |
Output is correct |
6 |
Correct |
27 ms |
1372 KB |
Output is correct |
7 |
Correct |
51 ms |
2396 KB |
Output is correct |
8 |
Correct |
46 ms |
7124 KB |
Output is correct |
9 |
Correct |
46 ms |
6484 KB |
Output is correct |
10 |
Correct |
32 ms |
4200 KB |
Output is correct |
11 |
Correct |
32 ms |
4188 KB |
Output is correct |
12 |
Correct |
14 ms |
2652 KB |
Output is correct |
13 |
Correct |
36 ms |
5456 KB |
Output is correct |
14 |
Correct |
54 ms |
9044 KB |
Output is correct |
15 |
Correct |
57 ms |
7132 KB |
Output is correct |
16 |
Correct |
43 ms |
6480 KB |
Output is correct |
17 |
Correct |
35 ms |
4176 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |