이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define X first
#define Y second
#define INF 0x7f7f7f7f
int dp[2005];
vector<pair<int, int>> pos;
vector<int> zero;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	fill(dp, dp+n+1, -INF);
	for(int i = 0; i < n; i++)
	{
		int a, b;
		cin >> a >> b;
		if(a > 0)
			pos.push_back({a, b});
		else
			zero.push_back(b);
	}
	dp[1] = 0;
	for(int i = 0; i < (int)pos.size(); i++)
	{
		for(int j = n; j >= max(1, n-pos[i].X+1); j--)
			if(dp[j] != -INF)
				dp[n] = max(dp[n], dp[j] + pos[i].Y);
		for(int j = n-1; j >= max(1, pos[i].X-1); j--)
			if(dp[j-pos[i].X+1] != -INF)
				dp[j] = max(dp[j], dp[j-pos[i].X+1] + pos[i].Y);
	}
	sort(zero.begin(), zero.end(), greater<>());
	int ans = 0;
	int sum = 0;
	for(int i = 1; i <= n; i++)
	{
		if(i <= (int)zero.size() && zero[i-1] >= 0)
			sum += zero[i-1];
		if(dp[i] != -INF)
			ans = max(ans, dp[i] + sum);
	}
	if(!zero.empty())
		ans = max(ans, zero[0]);
	cout << ans << '\n';
}
| # | 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... |