# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
838860 | Achilles1 | Knapsack (NOI18_knapsack) | C++17 | 35 ms | 63060 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define Sellihca ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long ll;
#define M 1000000007
const int N = 1e5 + 2;
struct p {
int val, w, c;
};
void Sellihca1() {
int n, s;
int val, w, c;
int mxw = 0;
cin >> s >> n;
vector<pair<int, int>> weights[2000 + 1];
for (int i = 1; i <= n; ++i) {
cin >> val >> w >> c;
mxw = max(mxw, w);
c = min(c, 2000);
weights[w].emplace_back(val, c);
}
ll p[2001][2001];
memset(p, 0, sizeof p);
for (int i = 0; i <= 2000; ++i) {
std::sort(weights[i].begin(), weights[i].end(), greater<>());
}
for (int i = 0; i <= 2000; ++i) {
int idx = 0;
for (int j = 0; idx < weights[i].size() && j < 2000; ++j) {
p[i][j] = weights[i][idx].first;
weights[i][idx].second--;
if (!weights[i][idx].second) idx++;
}
for (int j = 1; j <= 2000; ++j) {
p[i][j] += p[i][j - 1];
}
}
ll dp[2001][2001];
memset(dp, -1, sizeof dp);
dp[0][0] = 0;
for (int weight = 1; weight <= mxw; ++weight) {
for (int pick = s; pick >= 0; --pick) {
for (int cnt = 1; cnt <= s / weight; ++cnt) {
dp[weight][pick] = max(dp[weight][pick], dp[weight - 1][pick]);
if (pick - weight * cnt >= 0 && ~(dp[weight - 1][pick - weight * cnt]))
dp[weight][pick] = max(dp[weight][pick], dp[weight - 1][pick - weight * cnt] + p[weight][cnt-1]);
}
}
}
ll ans = 0;
for (int i = 1; i <= 2000; ++i) {
ans = max(ans, dp[i][s]);
}
cout << ans;
}
int main() {
Sellihca
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t = 1;
// cin >> t;
while (t--) {
Sellihca1();
}
}
컴파일 시 표준 에러 (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... |