Submission #563971

#TimeUsernameProblemLanguageResultExecution timeMemory
563971NeriWKnapsack (NOI18_knapsack)C++17
29 / 100
543 ms3456 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, int> pill;
typedef vector<pill> vpill;
typedef vector<vpill> vvpill;
int main()
{
    //freopen("..\\io\\input.txt", "r", stdin);
    //freopen("..\\io\\output.txt", "w", stdout);
    int s, n; cin >> s >> n;
    vll k(n+1, 0);
    vll v(n+1, 0);
    vll w(n+1, 0);
    vvpill dp(n+1, vpill(s+1, {0,0}));
    for(int i = 1; i <= n; i++)
    {
        cin >> v[i] >> w[i] >> k[i];
    }
    for(int i = 1; i <= n; i++)
    {
        for(int m = 1; m <= w[i]; m++)
        {
            for(int j = m; j <= s; j+=w[i])
            {
                /*if(i == 1 && j == s)
                {
                    cout << "hello\n";
                    cout << dp[i][j].first << "; " << dp[i][j].second << "\n";
                }*/
                if(dp[i][j- w[i]].second < k[i] && j  - w[i] >= 0)
                {
                    if(dp[i][j].first < dp[i][j-w[i]].first + v[i] || (dp[i][j].first == dp[i][j-w[i]].first + v[i] && dp[i][j-w[i]].second + 1 < dp[i][j].second))
                    {
                        dp[i][j] = {dp[i][j-w[i]].first + v[i], dp[i][j-w[i]].second + 1};
                    }
                }
                if(j  - w[i] >= 0)
                {
                    if(dp[i][j].first < dp[i-1][j-w[i]].first + v[i] || (dp[i][j].first == dp[i-1][j-w[i]].first + v[i] && dp[i][j].second > 1))
                    {
                        dp[i][j] = {dp[i-1][j-w[i]].first + v[i], 1};
                    }
                }
                if(dp[i][j].first <= dp[i-1][j].first)
                {
                    dp[i][j] = dp[i-1][j];
                    dp[i][j].second = 0;
                }
                /*if(i == 1 && j == s)
                {
                    cout << "hello\n";
                    cout << dp[i][j-1].first << "; " << dp[i][j-1].second << "\n";
                }*/
                if(dp[i][j].first < dp[i][j-1].first || (dp[i][j].first == dp[i][j-1].first && dp[i][j].first > dp[i][j-1].second))
                {
                    dp[i][j] = dp[i][j-1];
                    
                }
            }
            for(int j = 1; j <= s; j++)
            {
                dp[i][j] = max(dp[i][j], dp[i][j-1]);
            }
               
        }
    }
    /*for(int i = 1; i <= n; i++)
    {
        for(int j = 0; j <= s; j++)
        {
            cout << dp[i][j].first << ", " << dp[i][j].second << " -> ";
        }
        cout << "\n";
    }*/
    cout << dp[n][s].first;

}
#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...