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;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define int long long
typedef pair<int, int> ii;
const int N = 2e3 + 5;
const int inf = 1e14;
int n, dp[N][N];
ii a[N];
signed main() {
    cin.tie(0), ios_base::sync_with_stdio(0);
    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> a[i].fi >> a[i].se;
    sort(a + 1, a + 1 + n);
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++)
            dp[i][j] = -inf;
    }
    dp[0][0] = 0;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j <= n; j++) {
            if(dp[i][j] == -inf) continue;
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
            if(a[i + 1].fi == 0)
                dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + a[i + 1].se);
            if(a[i + 1].fi)
                dp[i + 1][max(j - a[i + 1].fi + 1, 1ll)] = max(dp[i + 1][max(j - a[i + 1].fi + 1, 1ll)], dp[i][j] + a[i + 1].se);
        }
    }
    cout << max(dp[n][1], 0ll);
}
| # | 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... |