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;
#define ll long long
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef vector<pll> vll;
typedef vector<vl> vvl;
#define fori(i, n) for (int i = 0; i < n; i++)
#define ford(i, n) for (int i = n - 1; i >= 0; i--)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define repd(i, a, b) for (int i = a; i >= b; i--)
#define trav(x, a) for (auto &x : a)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define endl '\n'
#define sz(a) (int)(a).size()
#define fi first
#define se second
#define mp make_pair
void solve(){
int s,n;
cin>>s>>n;
vi v(n);
vi w(n);
vi k(n);
fori(i,n){
cin>>v[i]>>w[i]>>k[i];
}
map<int, vii> m;
fori(i,n){
m[w[i]].pb({v[i],k[i]});
}
vvi dp(2,vi(s+1,0));
int h = m.size();
int l=1;
int i=0;
for(auto x:m){
int wt = x.fi;
// cout<<wt<<" c";
l=!l;
vii temp = m[wt];
sort(all(temp),greater<pii>());
fori(g,sz(temp)){
if(g!=0)
temp[g].se += temp[g-1].se;
// cout<<temp[0].fi<<" "<<temp[0].se<<endl;
}
int p=0,t=0;
rep(j,1,s){
dp[!l][j] = dp[l][j];
if(j>=wt){
if(j-p*wt>=wt){
p++;
}
while(t<sz(temp) && p>temp[t].se){
t++;
}
if(t<sz(temp))
dp[!l][j] = max({dp[!l][j], dp[!l][j-wt]+temp[t].fi, dp[l][j-wt]+temp[0].fi});
else
dp[!l][j] = max({dp[!l][j], dp[l][j-wt]+temp[0].fi});
}
// cout<<dp[!l][j]<<" ";
}
// cout<<endl;
i++;
}
int ans=0;
fori(i,s+1){
ans = max(ans,dp[h%2][i]);
}
// fori(i,n+1){
// fori(j,s+1){
// cout<<dp[i][j]<<" ";
// }
// cout<<endl;
// }
cout<<ans<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t=1;
// cin >> t;
while (t--)
{
solve();
}
}
# | 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... |