# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1193299 | dprto | Knapsack (NOI18_knapsack) | C++20 | 2 ms | 324 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 >> w >> n;
for (int i = 1; i <= n; i++)
cin >> c[i] >> W[i] >> a[i];
cout << ans(n, w, c, W, a);
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |