이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector <ll>
#define vii vector <pair<ll,ll>>
#define ii pair<ll,ll>
#define loop(n) for(ll i=0;i<n;i++)
#define loopj(n) for(ll j=0;j<n;j++)
#define pb push_back
#define ll_SIZE 60
const ll MAXDIV = 2000000000000LL;
ll mod=1e9+7;
const ll N=5e5+5;
ll n,m,k;
ll a,b;
ll dp[2005][2005];
vector<pair<ll,ll>> mp[2005];
ll solve(ll idx,ll we){
if(idx>m) return 0;
ll &ret=dp[idx][we];
if(~ret)return ret;
ret=solve(idx+1,we);
ll tt=0;
for (ll i = 0; i < mp[idx].size() && we+idx<=m; ++i) {
ll tmp= mp[idx][i].second;
while(we+idx<=m && tmp>0){
tt+=mp[idx][i].first;
we+=idx;
ret=max(ret,tt+solve(idx+1,we));
tmp--;
}
}
return ret;
}
int main()
{
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
// cout.tie(NULL);
ll tt=1;
// cin >>tt;
while(tt--){
cin >>m >> n;
memset(dp,-1,sizeof(dp));
loop(n){
ll v,w,k;
cin >> v >>w >>k;
mp[w].emplace_back(v,k);
}
for (ll i = 1; i <= m; ++i) {
sort(mp[i].begin(),mp[i].end());
reverse(mp[i].begin(),mp[i].end());
}
cout <<solve(0,0) <<endl;
}
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'long long int solve(long long int, long long int)':
knapsack.cpp:25:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (ll i = 0; i < mp[idx].size() && we+idx<=m; ++i) {
| ~~^~~~~~~~~~~~~~~~
# | 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... |