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;
using ii = pair<int,int>;
const int ilb = INT_MIN;
int dp[2005][2005], vis[2005][2005];
ii f[2005];
int n;
void input(){
cin>>n;
for(int i=0;i<n;i++) cin>>f[i].first>>f[i].second;
for(int i=0,j=n-1;i<j;){
for(;i<j&&f[i].first;++i);
for(;i<j&&!f[j].first;--j);
swap(f[i],f[j]);
}
for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) dp[i][j]=ilb;
dp[0][1]=0;
}
void work(){
for(int i=0;i<n;i++){
dp[i+1][0]=dp[i][0];
for(int j=1,k;j<=n;j++){
dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
k=min(n,j+f[i].first-1);
if(dp[i][j]!=ilb) dp[i+1][k]=max(dp[i+1][k],dp[i][j]+f[i].second);
}
}
}
int main(){
cin.sync_with_stdio(false);cin.tie(0);
input();
work();
printf("%d\n",*max_element(dp[n],dp[n]+n+1));
}
# | 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... |