# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1015896 | coolsentenceidontremember | Knapsack (NOI18_knapsack) | C++17 | 75 ms | 35328 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
#define ll long long
#define ld long double
#define ff first
#define ss second
#define db double
#define time_begin() auto begin = chrono::high_resolution_clock::now()
#define time_end() auto end = chrono::high_resolution_clock::now(); auto elapsed = chrono::duration_cast<chrono::nanoseconds>(end-begin); auto sec = elapsed.count() * 1e-9; cerr << "\n\nExecution time: " << sec << " seconds";
#define check_time() cerr << "\nStatus : "; if (sec>1) cerr << "Time Limit Exceeded 1!!!1!111"; else cerr << "You good brother"
using namespace std;
void setIO(string s = ""){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
if (!s.empty()){
freopen((s+".in").c_str(), "r", stdin);
freopen((s+".out").c_str(), "w", stdout);
}
}
// use two dp array to save space
int main(){
setIO();
int s, n;
cin >> s >> n;
map<int, vector<pair<int, int>>> group;
for (int i = 1; i <= n; i++){
int v, w, k;
cin >> v >> w >> k;
group[w].push_back({v, k});
}
for (auto &[key, val] : group) sort(group[key].begin(), group[key].end(), greater<pair<int, int>>());
vector<vector<ll>> dp(group.size()+1, vector<ll>(s+1, -LLONG_MAX));
for (int i = 0; i <= s; i++) dp[0][i] = 0;
int cur = 0;
for (auto &[weight, items] : group){
cur++;
for (int i = 0; i <= s; i++){
dp[cur][i] = dp[cur-1][i];
int num = 0;
ll val = 0;
int sz = items.size();
int cur_item = 0;
int cnt = 0;
while (1LL*(num+1)*weight <= i && cur_item < sz && cnt < items[cur_item].ss){
num++;
cnt++;
val += items[cur_item].ff;
if (dp[cur-1][i-num*weight] != -LLONG_MAX) dp[cur][i] = max(dp[cur][i], dp[cur-1][i-num*weight] + val);
if (cnt == items[cur_item].ss){
cnt = 0;
cur_item++;
}
}
}
}
cout << *max_element(dp[cur].begin(), dp[cur].end()) << '\n';
}
컴파일 시 표준 에러 (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... |