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 <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--)
dp[n] = max(dp[n], dp[j] + pos[i].Y);
for(int j = n-1; j >= max(1, pos[i].X-1); j--)
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];
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... |