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>
#define int long long
using namespace std;
int dp[2005][2005];
pair<int, int> a[2005];
int maxdp[2005][2005];
signed main()
{
int n;
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>a[i].first>>a[i].second;
a[i].first--;
}
sort(a+1, a+n+1);
for(int i=1; i<=n/2; i++)
{
swap(a[i], a[n+1-i]);
}
for(int i=0; i<=n+1; i++)
{
dp[0][i]=-100000000000;
}
for(int i=n+1; i>1; i--)
{
maxdp[0][i]=-100000000000;
}
dp[0][1]=0;
for(int i=1; i<=n; i++)
{
for(int j=0; j<=n+1; j++)
{
dp[i][j]=dp[i-1][j];
}
for(int j=a[i].first+1; j<=n; j++)
{
dp[i][j]=max(dp[i][j], dp[i-1][j-a[i].first]+a[i].second);
}
dp[i][0]=max(dp[i-1][0], maxdp[i-1][1]+a[i].second);
dp[i][n]=max(dp[i][n], maxdp[i-1][max(1ll, n-a[i].first)]+a[i].second);
maxdp[i][n+1]=dp[i][n+1];
for(int j=n; j>=1; j--)
{
maxdp[i][j]=max(maxdp[i][j+1], dp[i][j]);
}
}
// for(int i=1; i<=n; i++)
// {
// for(int j=1; j<=n; j++)
// {
// cout<<dp[i][j]<<" ";
// }
// cout<<endl;
// }
int ans=0;
for(int i=0; i<=n; i++)
{
ans=max(ans, dp[n][i]);
}
cout<<ans;
}
# | 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... |