Submission #912359

#TimeUsernameProblemLanguageResultExecution timeMemory
912359vjudge1Potatoes and fertilizers (LMIO19_bulves)C++17
20 / 100
530 ms4696 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...