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 <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <utility>
#include <cmath>
#include <numeric>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 2007;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
vector < pair <ll,ll> > v[M];
ll n, s, dp[M][M];
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> s >> n;
for(int i = 1; i <= n; ++i){
ll val, w, k;
cin >> val >> w >> k;
v[w].pb({val, k});
}
for(int i = 1; i <= s; ++i){
sort(all(v[i]), greater < pair <ll,ll> > ());
ll w = 0, add = 0;
for(auto k : v[i]){
ll rem = k.S;
while(rem--){
w += i;
add += k.F;
if(w > s) break;
for(int j = w; j <= s; ++j)
dp[j][i] = max(dp[j][i], dp[j - w][i - 1] + add);
}
if(w > s) break;
}
for(int j = 1; j <= s; ++j) dp[j][i] = max(dp[j][i], dp[j][i - 1]);
}
ll ans = 0;
for(int i = 1; i <= 2000; ++i) ans = max(ans, dp[i][s]);
cout << ans << endl;
return 0;
}
// Don't forget special cases. (n = 1?)
// Look for the constraints. (Runtime array? overflow?)
# | 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... |