Submission #821769

#TimeUsernameProblemLanguageResultExecution timeMemory
821769ZobioKnapsack (NOI18_knapsack)C++14
100 / 100
52 ms36288 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<char> vc; typedef vector<string> vs; typedef vector<bool> vb; typedef long double ld; typedef vector<double> vd; typedef vector<long long> vl; typedef vector<vector<int>> vvi; typedef vector<vector<ll>> vvl; typedef vector<pair<int,int>> vpi; typedef vector<pair<ll,ll>> vpl; typedef pair<int,int> pi; typedef pair<ll,ll> pl; typedef set<int> si; typedef set<ll> sl; #define all(c) (c).begin(),(c).end() #define srt(c) sort(all(c)) #define srtrev(c) sort(all(c)); reverse(all(c)) #define fr(i,a,n) for( int i=a;i<n;i++) #define frev(i,a,n) for( int i=a;i>=n;i--) #define inp(ve) for(int i=0;i<ve.size();i++) cin>>ve[i]; #define PB push_back #define F first #define S second #define cont continue #define br break #define MP make_pair #define nl cout<<"\n" #define M 1000000007 const ll INF = 1e17; const ll NINF = -1e17; const int N = 1e6+1; int lindieq (int a, int b, int& x, int& y) { if (b == 0) { x = 1; y = 0; return a; } int x1, y1; int d = lindieq(b, a % b, x1, y1); x = y1; y = x1 - y1 * (a / b); return d; } ll gcd (ll c, ll d) { ll a = max(c,d); ll b = min(c,d); while (b) { a %= b; ll temp = a; a=b; b=temp; } return a; } ll bexp(ll a,ll b){ ll res = 1; while(b>0){ if(b%2) res = res*a; a*=a; b/=2; } return res; } vb isprime(int n){ vector<bool> is_prime(n+1, true); is_prime[0] = is_prime[1] = false; for (int i = 2; i * i <= n; i++) { if (is_prime[i]) { for (int j = i * i; j <= n; j += i) is_prime[j] = false; } } return is_prime; } ll bexpm(ll a,ll b){ ll res = 1; while(b>0){ if(b%2) res = (res*a)%M; a = (a*a)%M; b/=2; } return res; } // int par[N]; // int r[N]; // int find(int a){ // if(par[a]==a) return a; // else return par[a] = find(par[a]); // } // bool isSame(int a, int b){ // return find(a)==find(b); // } // void unite(int a, int b){ // a = find(a); // b = find(b); // if(r[a]>=r[b]){ // par[b] = a; // r[a]+=r[b]; // }else{ // par[a] = b; // r[b]+=r[a]; // } // } map<int,vpl> mp; int main(){ ios::sync_with_stdio(0); cin.tie(0); int t=1; // cin>>t; while(t--){ int s,n; cin>>s>>n; vector<vpl> w(s+1); fr(i,0,n){ int cost,weig,feq; cin>>cost>>weig>>feq; w[weig].push_back({cost,feq}); } fr(i,1,s+1){ srt(w[i]); reverse(all(w[i])); } vvl dp(s+1,vl(s+1,0)); fr(i,1,s+1){ dp[i] = dp[i-1]; ll cs = 0; ll we = 0; int ma = s/i; for(auto it : w[i]){ while(it.second && ma){ cs+=it.first; we+=i; ma--; it.second--; for(int j = we;j<=s;j++){ dp[i][j] = max(dp[i][j],dp[i-1][j-we]+cs); } } if(ma==0) break; } } ll ans = 0; fr(i,1,s+1){ ans = max(dp[s][i],ans); } cout<<ans; } return 0; }
#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...