# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1124266 | wrtt | Knapsack (NOI18_knapsack) | C++20 | 1090 ms | 2832 KiB |
#pragma GCC optimize("Ofast, unroll-loops")
#include<iostream>
#include<deque>
#include<cstring>
using namespace std;
#define int long long
#define pb emplace_back
#define mx(a, b) ((a) > (b) ? (a) : (b))
#define mn(a, b) ((a) < (b) ? (a) : (b))
#define graph(n) vector<int> g[(n)+1];
constexpr int mod = 998244353;
constexpr int inf = 1e18;
void frx(std::string name) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
/*
template <typename T, size_t N>
ostream &operator<<(ostream &os, const T (&arr)[N]) {
os << "[";
for(size_t i = 0; i < N; ++i) {
if(i>0) {
os << ", ";
}
os<<arr[i];
}
return os << "]";
}
*/
void solve() {
int s,n;
cin>>s>>n;
int v[n], w[n], k[n];
for(int i = 0; i < n; ++i) {
cin>>v[i]>>w[i]>>k[i];
}
if(n==1) {
cout<<(mn(s/w[0], k[0])*v[0])<<"\n";
return;
}
int dp[s+1];
memset(dp,0,sizeof(dp));
for(int i = 0; i < n; ++i) {
for(int mod = 0; mod < w[i]; ++mod) {
deque<pair<int,int>> q;
for(int j = mod, cnt = 0; j<=s; j+=w[i], ++cnt) {
if(!q.empty() && q.front().second < cnt-k[i]) {
q.pop_front();
}
int curr = dp[j] - cnt*v[i];
while(!q.empty() && q.back().first <= curr) {
q.pop_back();
}
q.emplace_back(curr,cnt);
dp[j] = q.front().first+cnt*v[i];
}
}
}
cout<<dp[s]<<"\n";
}
signed main() {
std::ios_base::sync_with_stdio(false);
//std::cin.tie(nullptr);
//frx("piggyback");
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
auto start = std::chrono::high_resolution_clock::now();
#endif
int t = 1;
//cin >> t;
int curr = 1;
while (t--) {
//cout<<"Case "<<curr<<"\n";
++curr;
solve();
}
#ifdef LOCAL
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
std::cout << elapsed.count() << " seconds\n";
#endif
return 0;
}
Compilation message (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... |