#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);
}
std::cerr << "a: " << ans << std::endl;
std::cerr << "D:"; for (int x: diff) std::cerr << " " << x; std::cerr << std::endl;
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
6 ms |
348 KB |
Output is correct |
3 |
Correct |
6 ms |
348 KB |
Output is correct |
4 |
Correct |
116 ms |
1028 KB |
Output is correct |
5 |
Correct |
204 ms |
1620 KB |
Output is correct |
6 |
Correct |
527 ms |
5460 KB |
Output is correct |
7 |
Execution timed out |
1039 ms |
10084 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
6 ms |
348 KB |
Output is correct |
3 |
Correct |
6 ms |
348 KB |
Output is correct |
4 |
Correct |
116 ms |
1028 KB |
Output is correct |
5 |
Correct |
204 ms |
1620 KB |
Output is correct |
6 |
Correct |
527 ms |
5460 KB |
Output is correct |
7 |
Execution timed out |
1039 ms |
10084 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
6 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
6 ms |
348 KB |
Output is correct |
3 |
Correct |
6 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
6 ms |
348 KB |
Output is correct |
3 |
Correct |
6 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |