이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast,O3,unroll-loops")
#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpi vector<pii>
#define vvpi vector<vpi>
#define vb vector<bool>
#define vvb vector<vb>
#define endl "\n"
#define sp << " " <<
#define F(i, s, n) for(int i = s; i < n; i++)
#define pb push_back
#define fi first
#define se second
const int mod = 1e8;
int inf = LLONG_MAX >> 4;
int add(int x, int y) {
return (x + y) % mod;
}
int mult(int x, int y) {
return ((x % mod) * (y % mod)) % mod;
}
vvpi W(2001);
void solve() {
int n, k;
cin >> k >> n;
vi v(n+1, 0), w(n+1, 0), amt(n+1, 0);
F(i, 1, n+1) cin >> v[i] >> w[i] >> amt[i];
F(i, 1, n+1) {
W[w[i]].pb({v[i], amt[i]});
}
F(i, 1, 2001) sort(W[i].begin(), W[i].end(), greater<pii>());
vvi dp(2001, vi(k+1, -inf));
dp[0][0] = 0;
F(i, 1, 2001) {
F(j, 0, k+1) {
dp[i][j] = dp[i-1][j];
int sumw = 0, sumv = 0;
for(auto c: W[i]) {
F(rep, 1, c.se+1) {
sumw += i, sumv += c.fi;
if(sumw > j) break;
dp[i][j] = max(dp[i][j], dp[i-1][j-sumw] + sumv);
}
if(sumw > j) break;
}
}
}
int ans = -inf;
F(i, 0, k+1) ans = max(ans, dp[2000][i]);
cout << ans << endl;
}
void setIO() {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef Local
freopen("local.in", "r", stdin);
freopen("local.out", "w", stdout);
#else
//freopen("snakes.in","r",stdin);
//freopen("snakes.out","w",stdout);
#endif
}
signed main() {
setIO();
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... |