제출 #1195612

#제출 시각아이디문제언어결과실행 시간메모리
1195612vnedu스트랩 (JOI14_straps)C++17
100 / 100
14 ms16252 KiB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

template<class T> bool maximize(T &a, const T &b){ return (a < b ? a = b, 1 : 0); }
template<class T> bool minimize(T &a, const T &b){ return (a > b ? a = b, 1 : 0); }

#define fi first
#define se second

#define pb push_back
#define ii pair<int, int>

#define all(x) x.begin(), x.end()

#define TASK "nonsense"

/// end of template ///

const int N = 2e3 + 10;
const int inf = INT_MAX-10;
struct Strap
{
    int a,b;
    Strap() {}
    void input()
    {
        cin>>a>>b;
        --a;
    }
    bool operator < (const Strap &S) const
    {
        if(a!=S.a) return a>S.a;
        return b>S.b;
    }
} strap[N];
int n,dp[N][N];
void solve()
{
    cin>>n;
    for(int i=1;i<=n;++i) strap[i].input();
    sort(strap+1,strap+1+n);
    for(int i=0;i<N;++i) for(int j=0;j<N;++j) dp[i][j]=-inf;
    dp[0][1]=0;
    int ans=-inf;
    for(int i=0;i<=n;++i) for(int j=1;j<=n;++j) if(dp[i][j]!=-inf)
    {
        if(strap[i].a>=0)
        {
            if(i==n || strap[i+1].a<0)
            {
                int sum=dp[i][j];
                for(int pos=i+1;pos<=min(n,i+j);++pos) sum+=max(0,strap[pos].b);
                maximize(ans,sum);
            }
            else
            {
                maximize(dp[i+1][min(n,j+strap[i+1].a)],dp[i][j]+strap[i+1].b);
                maximize(dp[i+1][j],dp[i][j]);
            }
        }
    }
    cout<<ans;
}
int main()  {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

//    freopen(TASK".inp","r",stdin);
//    freopen(TASK".out","w",stdout);

    int testcase=1;
//    cin>>testcase;

    while (testcase--)
        solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...