Submission #1193298

#TimeUsernameProblemLanguageResultExecution timeMemory
1193298daotuankhoiKnapsack (NOI18_knapsack)C++20
0 / 100
1 ms324 KiB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define task ""
#define mas(a, n) a + 1, a + n + 1
#define cts(a) a.begin(), a.end()
using namespace std;
const int narr = 1e2 + 5;

void testcase()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
}

void readfo()
{
    freopen(task".inp", "r", stdin);
    freopen(task".out", "w", stdout);
}

int ans(int n, int w, int W[], int c[], int a[])
{
    vector<int> dp(w + 1, 0);
    int wi, ci, ui, v;
    for (int i = 1; i <= n; i++)
    {
        wi = W[i], ci = c[i], ui = a[i];
        if (wi > w)
            continue;
        for (int j = 0; j < wi; j++)
        {
            deque<pair<int, int>> dq;
            for (int curw = j; curw <= w; curw += wi)
            {
                v = dp[curw] - (curw / wi) * ci;
                while (!dq.empty() && dq.back().first <= v)
                    dq.pop_back();
                dq.emplace_back(v, curw);
                int maxi = ui * wi;
                if (!dq.empty() && dq.front().second < curw - maxi)
                    dq.pop_front();
                dp[curw] = dq.front().first + (curw / wi) * ci;
            }
        }
    }
    return dp[w];
}

int W[narr], c[narr], a[narr], n, w;

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    testcase();
    // readfo();
    cin >> n >> w;
    for (int i = 1; i <= n; i++)
        cin >> W[i] >> c[i] >> a[i];
    cout << ans(n, w, W, c, a);
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'void testcase()':
knapsack.cpp:13:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:14:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |     freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp: In function 'void readfo()':
knapsack.cpp:20:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |     freopen(task".inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:21:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |     freopen(task".out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...