Submission #759835

#TimeUsernameProblemLanguageResultExecution timeMemory
759835Doncho_BonbonchoKnapsack (NOI18_knapsack)C++14
100 / 100
76 ms47168 KiB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
 
#define endl "\n"
 
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
 
#ifndef LOCAL
#define cerr if(false) cerr
#define endl "\n"
#endif
 
template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {
    return os << "(" << p.first << ", " << p.second << ")";
}
template<typename T, typename B = decay<decltype(*begin(declval<T>()))>, typename enable_if<!is_same<T, string>::value>::type* = nullptr>
ostream& operator <<(ostream &os, const T &c) {
    bool f = false;
    os << "(";
    for(const auto &x : c) {
        if(f) os << ", "; f = true;
        os << x;
    }
    return os << ")";
}
 
 
#define out(x) #x << "=" << x << " "
struct debug {
    debug(const string &msg) { cerr << "LINE " << msg <<  ": "; }
    ~debug() { cerr << endl; }
 
    template<class T>
    debug& operator <<(const T x) {
        cerr << x;
        return *this;
    }
};
#define dbg debug(to_string(__LINE__))
 
typedef long long ll;
const ll mod = 1e9 + 7;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

//ll dpNow[2001];
//ll dpPrev[2001];
ll dp[2001][100001];
int v[100001];
int val[100001];
int num[100001];
std::vector<std::pair<int, int>> items[2001];

signed main() {
#ifndef LOCAL
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#endif
	
	int n, s;
	std::cin>>s>>n;
	for( int i=1 ; i<=n ; i++ ){
		std::cin>>val[i]>>v[i]>>num[i];
		if( v[i] <= s ) items[v[i]].push_back({ val[i], num[i] });
	}

	int indItem = 1;
	for( int i=1 ; i<=s ; i++ ){
		cerr<<endl<<out( i )<<endl;
		std::sort( items[i].begin(), items[i].end(), []( std::pair<int, int> &a, std::pair<int, int> &b ){
				return a > b;
				});
		for( auto j : items[i] ) cerr<<out(j)<<" ";
		cerr<<endl;
		for( int j=0 ; j<=s ; j++ ){
			cerr<<out( j )<<endl;
			chkmax( dp[indItem][j], dp[indItem-1][j] );
			if( j ) chkmax( dp[indItem][j], dp[indItem][j-1] );

			int ind = 0;
			int num = 0;
			ll currS = 0;
			int allItems = 0;
			while( j - ( allItems + 1 ) * i >= 0 and ind < items[i].size() ){
				currS += items[i][ind].first;
				allItems ++;
				num++;
				cerr<<out(currS)<<" "<<out( allItems )<<" "<<out( num )<<" "<<out( ind )<<endl;
				if( chkmax( dp[indItem][j], dp[indItem-1][j-allItems*i] + currS ) ) cerr<<" new dp "<<endl;
				if( num == items[i][ind].second ){
					num = 0;
					ind ++;
				}
			
			}
			cerr<<out( dp[indItem][j] )<<endl;
		}
		indItem ++;
	}

	std::cout<<dp[s][s]<<endl;
				
	/*
	for( int i=1 ; i<=n ; i++ ){
		for( int j=0 ; j<=s ; j++ ){
			chkmax( dpNow[j], dpPrev[j] );
			if( j ) chkmax( dpNow[j], dpNow[j-1] );
			for( int k=1 ; k<=num[i] ; k++ ){
				cerr<<" ! "<<k<<" "<<v[i]*k<<endl;
				if( j- v[i]*k < 0 ) break;
				chkmax( dpNow[j], dpPrev[j-k*v[i]] + k*val[i] );
			}
			cerr<<i<<" "<<j<<" "<<" -> "<<dpNow[j]<<endl;
		}
		for( int j=0 ; j<=s ; j++ ){
			dpPrev[j] = dpNow[j];
		}
	}
			
	std::cout<<dpPrev[s]<<endl;
	*/
 
    return 0;
}

// To the OGs, I'm thankin' you now
// Was watchin' you when you was pavin' the ground
// I copied your cadence, I mirrored your style
// I studied the greats, I'm the greatest right now

Compilation message (stderr)

knapsack.cpp: In function 'std::ostream& operator<<(std::ostream&, const T&)':
knapsack.cpp:24:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   24 |         if(f) os << ", "; f = true;
      |         ^~
knapsack.cpp:24:27: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   24 |         if(f) os << ", "; f = true;
      |                           ^
knapsack.cpp: In function 'int main()':
knapsack.cpp:86:49: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |    while( j - ( allItems + 1 ) * i >= 0 and ind < items[i].size() ){
      |                                             ~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...