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 <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <math.h>
using namespace std;
typedef long long llong;
typedef pair<int, int> pi;
int n;
const int inf = -2147483648;
int as[2000];
int bs[2000];
int dp[2000][2000];
int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> n;
	int x, a, b;
	for (int i = 0; i < n; ++i) {
		cin >> as[i] >> bs[i];
	}
	int i = 0, j = n - 1;
	while (i < j) {
		while (i < j && as[i] != 0) ++i;
		while (i < j && as[j] == 0) --j;
		if (i < j) {
			x = as[i]; as[i] = as[j]; as[j] = x;
			x = bs[i]; bs[i] = bs[j]; bs[j] = x;
		}
	}
	for (int i = 0; i < n; ++i) {
		a = as[i]; b = bs[i];
		if (i == 0) {
			for (int j = 0; j < n; ++j) dp[0][j] = inf;
			dp[0][1] = 0;
			if (a < n)
				dp[0][a] = max(dp[0][a], b);
			else
				dp[0][n - 1] = max(dp[0][n - 1], b);
		}
		else {
			dp[i][0] = dp[i - 1][0];
			for (int j = 1; j < n - i; ++j) dp[i][j] = inf;
			for (int j = 1; j <= n - i; ++j) {
				if (dp[i - 1][j] == inf) continue;
				if (j < n - i)
					dp[i][j] = max(dp[i][j], dp[i - 1][j]);
				else
					dp[i][j - 1] = max(dp[i][j - 1], dp[i - 1][j]);
				x = j + a - 1;
				if (x < n - i) {
					dp[i][x] = max(dp[i][x], dp[i - 1][j] + b);
				}
				else {
					dp[i][n - i - 1] = max(dp[i][n - i - 1], dp[i - 1][j] + b);
				}
			}
		}
	}
	printf("%d\n", dp[n - 1][0]);
	return 0;
}
| # | 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... |