//#pragma GCC optimize(3)
#define NDEBUG
#include <bits/stdc++.h>
using namespace std;
struct item{
long long value;
int weight;
long long copies;
};
pair<long long,int> pseudoitems[3000000];
pair<long long,int> *pi_end; // value, weight
item items[100000];
item *items_end;
long long gcd(long long a, long long b){
if(a==0)return b;
if(b==0)return a;
return gcd(b,a%b);
}
int main(){
ios_base::sync_with_stdio(false);
//cin.tie(nullptr);
//cout.tie(nullptr);
int s,n;
while(cin>>s>>n&&s){
assert(s>=1&&s<=2000);
assert(n>=1&&n<=100000);
pi_end=pseudoitems;
for(int i=0;i<n;++i){
cin>>items[i].value>>items[i].weight>>items[i].copies;
assert(items[i].value>=1&&items[i].value<=1000000);
assert(items[i].weight>=1&&items[i].weight<=s);
assert(items[i].copies>=1&&items[i].copies<=1000000000);
/*int x=(int)gcd(items[i].value,items[i].weight);
items[i].value/=x;
items[i].weight/=x;
items[i].copies*=x;*/
}
sort(items,items+n,[](const item& a, const item& b){
if(b.weight==a.weight)return a.value>b.value;
return a.weight<b.weight;
});
items_end=items+n;
/*items_end=remove_if(items,items+n,[](const item& itm){
//return itm.value<30;
return itm.value/itm.weight<4000;
});*/
auto it=items+1;
auto it_end=items_end;
items_end=items;
int ct=0;
//cout<<"test"<<endl;
for(;it!=it_end;++it){
item itm=*it;
if(itm.weight==items_end->weight){
if(ct>s/itm.weight);//items_end->copies+=itm.copies;
else *(++items_end)=itm;
++ct;
//items_end->value=max(items_end->value,it->value);
}
else{
*(++items_end)=itm;
ct=0;
}
}
//cout<<"test"<<endl;
++items_end;
for(auto it=items;it!=items_end;++it){
const item& itm=*it;
int p;
for(p=0;(1ll<<(p+1))<=itm.copies;++p){
if((long long)itm.weight*(1ll<<p)<=s){
*pi_end++=make_pair(itm.value*(1ll<<p),itm.weight*(1ll<<p));
}
}
long long cp=itm.copies-((1ll<<p)-1);
if((long long)itm.weight*cp<=s){
*pi_end++=make_pair(itm.value*cp,itm.weight*cp);
}
}
//cout<<"test"<<endl;
long long* prev=new long long[s+1];
fill_n(prev,s+1,0);
for(auto it=pseudoitems;it!=pi_end;++it){
long long* curr = new long long[s+1];
for(int j=0;j<=s;++j){
curr[j]=prev[j];
if(j+it->second<=s){
curr[j]=max(curr[j],prev[j+it->second]+it->first);
}
}
delete[] prev;
prev=curr;
}
//cout<<"test"<<endl;
cout<<prev[0]<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
380 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
380 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
380 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
380 KB |
Output is correct |
26 |
Correct |
8 ms |
376 KB |
Output is correct |
27 |
Correct |
5 ms |
380 KB |
Output is correct |
28 |
Correct |
5 ms |
408 KB |
Output is correct |
29 |
Correct |
5 ms |
376 KB |
Output is correct |
30 |
Correct |
5 ms |
376 KB |
Output is correct |
31 |
Correct |
5 ms |
376 KB |
Output is correct |
32 |
Correct |
5 ms |
380 KB |
Output is correct |
33 |
Correct |
5 ms |
376 KB |
Output is correct |
34 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
380 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
380 KB |
Output is correct |
26 |
Correct |
8 ms |
376 KB |
Output is correct |
27 |
Correct |
5 ms |
380 KB |
Output is correct |
28 |
Correct |
5 ms |
408 KB |
Output is correct |
29 |
Correct |
5 ms |
376 KB |
Output is correct |
30 |
Correct |
5 ms |
376 KB |
Output is correct |
31 |
Correct |
5 ms |
376 KB |
Output is correct |
32 |
Correct |
5 ms |
380 KB |
Output is correct |
33 |
Correct |
5 ms |
376 KB |
Output is correct |
34 |
Correct |
5 ms |
376 KB |
Output is correct |
35 |
Correct |
41 ms |
2680 KB |
Output is correct |
36 |
Correct |
101 ms |
3064 KB |
Output is correct |
37 |
Correct |
47 ms |
2680 KB |
Output is correct |
38 |
Correct |
46 ms |
2724 KB |
Output is correct |
39 |
Correct |
53 ms |
2680 KB |
Output is correct |
40 |
Correct |
182 ms |
3448 KB |
Output is correct |
41 |
Correct |
81 ms |
2936 KB |
Output is correct |
42 |
Correct |
99 ms |
3076 KB |
Output is correct |
43 |
Correct |
128 ms |
3192 KB |
Output is correct |
44 |
Correct |
148 ms |
3320 KB |
Output is correct |
45 |
Correct |
72 ms |
2808 KB |
Output is correct |
46 |
Correct |
41 ms |
2680 KB |
Output is correct |
47 |
Correct |
89 ms |
2936 KB |
Output is correct |
48 |
Correct |
129 ms |
3192 KB |
Output is correct |
49 |
Correct |
45 ms |
2680 KB |
Output is correct |
50 |
Correct |
272 ms |
3960 KB |
Output is correct |