Submission #744381

#TimeUsernameProblemLanguageResultExecution timeMemory
744381vjudge1Knapsack (NOI18_knapsack)C++17
100 / 100
77 ms36428 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> ii;
#define ff first
#define ss second
const int mod = 1e9 + 7;
const int maxn = 2010;
int n,S;
vector <ii> a[maxn];
int dp[maxn][maxn];
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cin>>S>>n;
    for (int i=1; i<=n; i++)
    {
        int v,w,k; cin>>v>>w>>k;
        a[w].push_back({v,k});
    }
    for (int i=0; i<=S; i++) for (int j=0; j<=S; j++) dp[i][j] = 0;
    for (int i=1; i<=S; i++)
    {
        sort(a[i].begin(),a[i].end(),greater<ii>());
        for (int j=1; j<=S; j++)
        {
            dp[i][j] = dp[i-1][j];
            if (a[i].empty()) continue;
            int it, sum, used, val;
            it = 0;
            sum = a[i][0].ss;
            used = val = 0;
            while (it < a[i].size() && j-i*(used+1) >=0)
            {
                sum--; used++; val += a[i][it].ff;
                dp[i][j] = max(dp[i][j],dp[i-1][j-i*used] + val);
//                cout<<i<<' '<<j<<" := "<<used<<' '<<val<<'\n';
                if (sum == 0)
                {
                    it++;
                    if (it==a[i].size()) break;
                    sum = a[i][it].ss;
                }
            }
        }
        if (a[i].empty()) continue;
//        cout<<i<<" := ";
//        for (int j=1; j<=S; j++) cout<<dp[i][j]<<' '; cout<<'\n';
    }
    int ans = 0;
    for (int j=0; j<=S; j++) ans = max(ans,dp[S][j]);
    cout<<ans<<'\n';
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:34:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |             while (it < a[i].size() && j-i*(used+1) >=0)
      |                    ~~~^~~~~~~~~~~~~
knapsack.cpp:42:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |                     if (it==a[i].size()) break;
      |                         ~~^~~~~~~~~~~~~
#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...