# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1126503 | mazen_ghanayem | Knapsack (NOI18_knapsack) | C++20 | 252 ms | 327680 KiB |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ull unsigned long long
#define ld long double
#define int long long
#define nl "\n"
#define oo 1e9 + 1
#define OO 1e18 + 1
#define sp ' '
#define sz(x) (int)(x.size())
#define MOD 1000000007
#define fixed(n) fixed << setprecision(n)
#define sub_mod(a, b, m) ((((a) % m) - ((b) % m) + m) % m)
#define add_mod(a, b, m) ((((a) % m) + ((b) % m)) % m)
#define mult_mod(a, b, m) ((((a) % m) * ((b) % m)) % m)
#define EPS 1e-9
#define PI acos(-1)
using namespace __gnu_pbds;
using namespace std;
void fastio()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif
}
void solve(int tc)
{
int s, n;
cin >> s >> n;
vector<int> w(n * 12 + 5);
vector<int> v(n * 12 + 5);
int cnt = 1;
while (n--)
{
int weight, value, q;
cin >> value >> weight >> q;
int x = s / weight;
q = min(q, x + 1);
int k = 1;
while (k <= q)
{
w[cnt] = weight * k;
v[cnt] = value * k;
q -= k;
k *= 2;
cnt++;
}
if (q > 0)
{
w[cnt] = weight * q;
v[cnt] = value * q;
cnt++;
}
}
n = cnt;
vector<vector<int>> dp(n + 5, vector<int>(s + 5));
dp[0].assign(s + 5, 0);
for (int i = 1; i <= n; i++)
{
for (int ww = 0; ww <= s; ww++)
{
dp[i][ww] = dp[i - 1][ww];
if (ww >= w[i])
dp[i][ww] = max(dp[i][ww], dp[i - 1][ww - w[i]] + v[i]);
}
}
cout << dp[n][s] << nl;
}
signed main(void)
{
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
//fastio();
int tc = 1;
// cin >> tc;
int i = 1;
while (tc--)
{
// cout<<"Case #"<<i<<": ";
solve(i++);
}
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... |