#include <bits/stdc++.h>
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define pii pair<int , int>
#define pli pair<ll , int>
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define MASK(i) (1ll << i)
#define bit(mask , i) ((mask >> i) & 1)
#define debug cout << "NOBUG" << endl;
#define fi first
#define se second
#define i128 __int128
#define ldb long double
#define el "\n"
#define pb push_back
#define ALL(vec) vec.begin(),vec.end()
#define SORT_AND_UNIQUE(vec) sort(ALL(vec)); vec.erase(unique(ALL(vec)) , vec.end())
#define lb(vec , x) lower_bound(ALL(vec) , x)
using namespace std;
const ll oo = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7 , MOD = 102e6 + 100829;
void File(string NAME) {
freopen((NAME + ".inp").c_str() , "r" , stdin);
freopen((NAME + ".out").c_str() , "w" , stdout);
}
template <class X , class Y>
bool maximize(X &x , Y y) {
if(x < y) {
x = y;
return true;
}
return false;
}
template <class X , class Y>
bool minimize(X &x , Y y) {
if(x > y) {
x = y;
return true;
}
return false;
}
template <class X , class Y>
void add_mod(X &x , Y y ) {
x += y;
if(x >= mod) x -= mod;
if(x < 0) x += mod;
}
const int Nlimit = 1e5+5 , Slimit = 2000+3;
int n , S , v[Nlimit] , w[Nlimit] , k[Nlimit];
ll f[2][Slimit];
deque <int> dq[Slimit];
int main()
{
fast
//File("TASK");
cin >> S >> n;
for(int i = 1 ; i <= n ; i++) cin >> v[i] >> w[i] >> k[i];
for(int i = 1 ; i <= n ; i++) {
for(int s = 0 ; s < w[i] ; s++) dq[s].clear();
for(int s = 0 ; s <= S ; s++) {
f[0][s] = f[1][s] - 1ll*(s/w[i])*v[i];
int d = s % w[i];
deque <int> &dqd = dq[d];
while( dqd.size() && (s-dqd.front())/w[i] > k[i] ) dqd.pop_front();
while( dqd.size() && f[0][s] >= f[0][dqd.back()] ) dqd.pop_back();
dqd.push_back(s);
int sp = dqd.front();
f[1][s] = 1ll*(s/w[i])*v[i] + f[0][sp];
}
}
for(int s = 1 ; s <= S ; s++) maximize(f[1][s] , f[1][s-1]);
cout << f[1][S];
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'void File(std::string)':
knapsack.cpp:26:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | freopen((NAME + ".inp").c_str() , "r" , stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen((NAME + ".out").c_str() , "w" , stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |