#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 100;
int n, m;
long long dp[60300];
long long d[60300];
int A, B;
int a[maxn], b[maxn];
int main() {
cin >> n;
for(int i=1; i<=n; i++) {
cin >> a[i] >> b[i];
A += a[i];
B += b[i];
}
if(A <= 30000) {
for(int j = 0; j <= 2*A; j++) {
dp[j] = 1e18;
}
dp[A] = 0;
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= 2 * A; j++) {
d[j] = dp[j] + abs(j-A);
dp[j] = 1e18;
}
for(int j = 0; j <= 2 * A; j++) {
int k = j + a[i] - b[i];
if(k >= 0 && k <= 2 * A) {
dp[k] = min(dp[k], d[j]);
}
}
for(int j = 2 * A-1; j >= 0; j--) {
dp[j] = min(dp[j+1], dp[j]);
}
}
cout << dp[A];
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Incorrect |
2 ms |
4444 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Incorrect |
2 ms |
4444 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
59 ms |
4444 KB |
Output is correct |
5 |
Correct |
260 ms |
4536 KB |
Output is correct |
6 |
Correct |
530 ms |
4548 KB |
Output is correct |
7 |
Correct |
257 ms |
4444 KB |
Output is correct |
8 |
Correct |
150 ms |
4548 KB |
Output is correct |
9 |
Correct |
515 ms |
4696 KB |
Output is correct |
10 |
Correct |
9 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Incorrect |
2 ms |
4444 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Incorrect |
2 ms |
4444 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |