Submission #1201884

#TimeUsernameProblemLanguageResultExecution timeMemory
1201884tdkhaiKnapsack (NOI18_knapsack)C++20
100 / 100
448 ms33232 KiB
/*
        _.-- ,.--.
      .'   .'     /
       @       |'..--------._
     /      \._/              '.
    /  .-.-                     \
   (  /    \                     \
   \\      '.                  | #
    \\       \   -.           /
     :\       |    )._____.'   \
      "       |   /  \  |  \    )
              |   |./'  :__ \.-'
              '--'
*/
#include<bits/stdc++.h>
#define ll long long
#define llll pair<int,int>
#define ii pair<int,int>
#define fi first
#define se second
#define FOR(i,l,r) for(int i=l;i<=r;i++)
#define FOD(i,r,l) for(int i=r;i>=l;i--)
#define ull unsigned long long
#define iii pair<int,ii>
#define iv pair<pii,ii>
#define db double
#define ld long double
#define pb push_back


using namespace std;

const int dx[]  = {1, -1, 0, 0};
const int dy[]  = {0, 0, -1, 1};
const int dxx[] = {1, 1, -1, -1, 2, 2, -2, -2};
const int dyy[] = {2, -2, 2, -2, 1, -1, 1, -1};
const ll INF=1e18;


ll s,n,ans;
ll dp[2001][2001];
map<int,vector<ii>> vec;
void solve()
{
    cin >> s >> n;
    for(int i=1;i<=n;i++)
    {
        ll v,w,k;
        cin >> v >> w >> k;
        vec[w].pb({v,k});
    }
    for(int i=0;i<=2000;i++)
    {
        for(int j=0;j<=2000;j++)
        {
            dp[i][j]=-INF;
        }
    }
    dp[0][0]=0;
    for(int weight=1;weight<=s;weight++)
    {
        sort(vec[weight].rbegin(),vec[weight].rend());
        for(int i=0;i<=s;i++)
        {
            dp[weight][i]=dp[weight-1][i];
            ll profit=0;
            int cnt=0,used=0;
            int pt=0;
            while(cnt*weight+weight<=i and pt<vec[weight].size())
            {
                cnt++;
                profit+=vec[weight][pt].fi;
//                cout << vec[weight][pt].fi << ' ';
                if(dp[weight-1][i-cnt*weight]!=-INF) dp[weight][i]=max(dp[weight][i],dp[weight-1][i-cnt*weight]+profit);
                used++;
                if(used==vec[weight][pt].se)
                {
                    used=0;
                    pt++;
                }
            }
        }
    }
    for(int i=0;i<=s;i++)
    {
//        cout << dp[s][i] << ' ';
        ans=max(ans,dp[s][i]);
    }
    cout << ans;
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//    freopen("tdk.inp","r",stdin);
//    freopen("tdk.out","w",stdout);
    int t=1;
    //cin >> t;
    while(t--)
    {
        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...
#Verdict Execution timeMemoryGrader output
Fetching results...