이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
Author:Matkap(prefix_sum)
*/
#include <bits/stdc++.h>
#include <array>
using namespace std;
#define int long long
#define endl "\n"
#define ar array
#define all(x) x.begin(),x.end()
const int INF = 1e17 , MOD = 1e9 + 7;
void setIO(string name = "")
{
if (name.size())
{
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
int mul(int a,int b,int mod = MOD)
{
a %= mod;
b %= mod;
return a * 1LL * b % mod;
}
int sum(int a,int b,int mod = MOD)
{
a %= mod;
b %= mod;
return (a + b + mod) % mod;
}
int binpow(int base,int power,int mod = MOD)
{
if(power == 1) return base;
if(power == 0) return 1;
if(power%2==1)
{
int a;
a = binpow(base,(power - 1)/2);
return mul(base, mul(a, a, mod), mod);
}
else
{
int a;
a = binpow(base,power/2);
return mul(a, a, mod);
}
}
int inv(int a,int mod = MOD)
{
a %= mod;
return binpow(a, mod - 2) % mod;
}
void solve()
{
int n,s;
cin >> s >> n;
vector<int> dp(s + 1, -INF), ndp(s + 1);
vector<ar<int,2>> weights[s + 1];
dp[0] = 0;
for(int i = 0;n > i;i++)
{
ndp = dp;
int value, weight, count;
cin >> value >> weight >> count;
weights[weight].push_back({value,count});
}
for(auto& it : weights) sort(all(it));
for(int i = 1;s >= i;i++)
{
ndp = dp;
int used_weight = 0, total_value = 0;
while(weights[i].size() && used_weight < s)
{
auto& [value, count] = weights[i].back();
used_weight += i;
total_value += value;
for(int j = used_weight;s >= j;j++)
{
ndp[j] = max(ndp[j], dp[j - used_weight] + total_value);
}
count--;
if(count == 0) weights[i].pop_back();
}
dp = ndp;
}
cout << *max_element(all(dp)) << endl;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tt;
tt=1;
//cin >> tt;
while(tt--) solve();
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'void setIO(std::string)':
knapsack.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:22:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |