This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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];
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |