# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1086673 | lemon4life | Knapsack (NOI18_knapsack) | C++17 | 56 ms | 36432 KiB |
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 pair <ll, ll> pii;
#define fi first
#define se second
bool TURN(bool &X){ if (X) return 0; X = 1; return 1;}
template <class X, class Y> bool mini(X &x, const Y &y){ if (x > y){ x = y; return true;} return false;}
template <class X, class Y> bool maxi(X &x, const Y &y){ if (x < y){ x = y; return true;} return false;}
const int S = 2e3 + 5;
ll dp[S][S];
vector <pii> g[S];
void solve(){
int s, n; cin >> s >> n;
for (ll i = 1, v, w, k; i <= n; ++i){
cin >> v >> w >> k;
g[w].push_back({-v, k});
}
memset(dp, -0x3f, sizeof dp);
dp[0][0] = 0;
ll ans = 0;
for (int i = 1; i <= s; ++i){
sort(g[i].begin(), g[i].end());
for (int j = s; j >= 0; --j){
dp[i][j] = dp[i - 1][j];
if (g[i].size() == 0) continue;
ll y = 0, cur = g[i][0].second, sum = 0;
for (int x = 1; j - x * i >= 0; ++x){
sum -= g[i][y].first;
maxi(dp[i][j], dp[i - 1][j - x * i] + sum);
cur--;
if (cur == 0){
if (y == g[i].size() - 1) break;
y++;
cur = g[i][y].second;
}
}
maxi(ans, dp[i][j]);
}
}
cout << ans;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen("BALO.INP", "r")){
freopen("BALO.INP", "r", stdin);
freopen("BALO.OUT", "w", stdout);
}
solve();
return 0;
}
Compilation message (stderr)
# | 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... |