# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1276750 | awmqwe | Knapsack (NOI18_knapsack) | C++20 | 55 ms | 32844 KiB |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define pb push_back
int s, n, ans;
const int N = 1e5 + 9, M = 1e6 + 9, E = 2e3 + 9;
struct road {
int v, w, k;
};
road r[N];
int dp[E][E];// xét tới số có w = i, giá trị lớn nhất của w = i là;
int pre[E][E];
// giá trị tối đa người vợ có thể lấy với số kg i
int main (){
#define task ""
cin.tie (0) -> sync_with_stdio (0);
if (fopen (task ".inp", "r")){
freopen (task ".inp", "r", stdin);
freopen (task ".out", "w", stdout);
}
cin >> s >> n;
for (int i = 1; i <= n; i++){
cin >> r[i].v >> r[i].w >> r[i].k;
}
sort (r + 1, r + n + 1, [&] (road a, road b){
if (a.w == b.w) return a.v > b.v;
return a.w < b.w;
});
int j = 1;
// pre[i][j] là giá trị tối đa khi lấy j sản phẩm khối lượng i
for (int i = 1; i <= s; i++){
int now = 0;// số phần tử lấy
while (j <= n && r[j].w == i){
for (int k = 1; k <= r[j].k && now < s; k++){
pre[i][now + 1] = pre[i][now] + r[j].v;
now++;
}
j++;
}
}
for (int i = 1; i <= s; i++){ // xét các món hàng có w = s;
for (int j = 0; j <= i; j++) dp[i][j] = dp[i - 1][j];
for (int x = 1; x <= s / i; x++){ // số lượng
for (int j = i * x; j <= s; j++){// khối lượng
dp[i][j] = max ({dp[i][j], dp[i - 1][j - i * x] + pre[i][x], dp[i - 1][j]});
}
}
}
for (int i = 0; i <= s; i++) ans = max (ans, dp[s][i]);
cout << ans << '\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... |