# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
673707 | asteilind | Knapsack (NOI18_knapsack) | C++17 | 1085 ms | 3916 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 <iostream>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <set>
#include <numeric>
#include <bitset>
#include <climits>
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define ll long long
#define MOD 1000000007
using namespace std;
void setIO(string name = "") { // name is nonempty for USACO file I/O
ios_base::sync_with_stdio(0); cin.tie(0); // see Fast Input & Output
if(name.length()){
freopen((name+".in").c_str(), "r", stdin); // see Input & Output
freopen((name+".out").c_str(), "w", stdout);
}
}
void solve()
{
ll s,n; cin >> s >> n;
vector<ll> price(n),weight(n),count(n);
forn(i,n)
{
cin >> price[i] >> weight[i] >> count[i];
ll maxv = s/(weight[i]);
count[i] = min(count[i],maxv);
}
vector<ll> dp(s+1,-1);
dp[0] = 0;
ll res = 0;
for (ll a=0 ; n>a ; a++)
{
for (ll j = 0 ; count[a] > j ; j++)
{
for (ll i=s ; i >= 0; i--)
{
if (dp[i] != -1)
{
if (i+weight[a] > s) continue;
dp[i+weight[a]] = max(dp[i+weight[a]],dp[i]+price[a]);
res = max(res,dp[i+weight[a]]);
}
}
}
}
cout << res << endl;
}
int main()
{
ios_base::sync_with_stdio(0);
setIO();
solve();
}
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... |