This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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];
// }
// }
vi cost,w,num;
map<pi,ll> mp;
ll func(int ind,int cap){
if(ind==-1 || cap==0) return 0;
if(mp.find({ind,cap})!=mp.end()) return mp[{ind,cap}];
ll ans = func(ind-1,cap);
ll dcap = cap;
ll dc=0;
for(int i = 1;i<=num[ind];i++){
dc+=cost[ind];
dcap-=w[ind];
if(dcap<0) break;
ans = max(ans,func(ind-1,dcap)+dc);
}
mp[{ind,cap}] = ans;
return ans;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
// cin>>t;
while(t--){
int s,n;
cin>>s>>n;
cost.resize(n);
w.resize(n);
num.resize(n);
fr(i,0,n) cin>>cost[i]>>w[i]>>num[i];
cout<<func(n-1,s);
}
return 0;
}
# | 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... |