This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//SİGUSMUS SEFEROĞLU
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+50,inf = 2e9;
const int BUF_SZ=1<<16;
char buf[BUF_SZ];
int pos;
int len;
inline char next_char() {
if (pos == len) {
pos = 0;
len = (int)fread(buf, 1, BUF_SZ, stdin);
}
return buf[pos++];
}
inline int read_int() {
int x;
char ch=next_char();
x = ch - '0';
while (isdigit(ch = next_char())) { x = x * 10 + (ch - '0'); }
return x;
}
int alloc[2008];
int* ptr = alloc;
int sz = 0;
int v[N],k[N];
int sk[2008],sk1[2008];
vector<int>poss[2008];
signed main() {
#ifdef Dodi
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int w=read_int(),n=read_int();
int c;
for (int i=1;i<=n;i++) v[i]=read_int(),c=read_int(),k[i]=read_int(),poss[c].push_back(i);
int*sack=sk,*sack2=sk1;
for (int i=0;i<=w+1;i++) sack2[i] = -inf;
sack2[0] = 0;
for (int ii=1;ii<2008;ii++) {
if (w/ii > 1) {
int added=0;
int got = min<int>(poss[ii].size(),w/ii);
nth_element(poss[ii].begin(),poss[ii].begin()+got,poss[ii].end(),[&](const int x,const int y) {
return v[x] > v[y];
});
sort(poss[ii].begin(),poss[ii].begin()+got,[&](const int x,const int y) {
return v[x] > v[y];
});
for(int i:poss[ii]){
if(w<=added*ii)break;
added+=k[i];
int cool=0;
for (int jj = 0;jj<ii;jj++) {
sz = 0,ptr = alloc;
for (int j = jj;j<=w;j+=ii) {
if (sz && k[i] < (j-*ptr)/ii) {
sz--;
ptr++;
}
while (sz && (j-ptr[sz-1])/ii*v[i]+sack2[ptr[sz-1]] <= sack2[j]) {
sz--;
}
ptr[sz] = j;
sz++;
sack[j] = (j-*ptr)/ii*v[i]+sack2[*ptr];
cool|=sack[j]!=sack2[j];
}
}
if(!cool)break;
swap(sack,sack2);
}
}
else if(poss[ii].size()){
int i=0;
for(int j=1;j<poss[ii].size();j++){
if(v[poss[ii][i]]<v[poss[ii][j]])i=j;
}
i=poss[ii][i];
for (int jj = 0;jj<ii;jj++) {
sz = 0,ptr = alloc;
for (int j = jj;j<=w;j+=ii) {
if (sz && k[i] < (j-*ptr)/ii) {
sz--;
ptr++;
}
while (sz && (j-ptr[sz-1])/ii*v[i]+sack2[ptr[sz-1]] <= sack2[j]) {
sz--;
}
ptr[sz] = j;
sz++;
sack[j] = (j-*ptr)/ii*v[i]+sack2[*ptr];
}
}
swap(sack,sack2);
}
}
int ans=0;
for(int i=0;i<=w;i++)if(ans<sack2[i])ans=sack2[i];
cout << ans;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:84:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int j=1;j<poss[ii].size();j++){
| ~^~~~~~~~~~~~~~~~
# | 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... |