이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/////////////////////////////////////////////////////////////////
//////// Born_To_Laugh - Hughie Do ////////
/////////////////////////////////////////////////////////////////
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MAXN=3e5+5;
const int INF=1e9+7;
/*
Idea:
*/
void solve(){
map<int,vector<pair<int,int>>> wei;
int s, t;
cin >> s >> t;
while(t--){
int v, w, k;
cin >> v >> w >> k;
if(w <= s){
wei[w].push_back(make_pair(v,k));
}
}
vector<int> dp(s+1, 0);
for(int i=0; i<=s; ++i){
if(wei[i].size() == 0)continue;
sort(wei[i].begin(), wei[i].end(), greater<pair<int,int>>());
int index = 0;
//xet j so dau tien co can nang i
for(int j=0; j*i<=s; ++j){
if(index >= wei[i].size())break;
for(int k = s; k>=i; --k){
dp[k]=max(dp[k], dp[k-i]+wei[i][index].first);
}
wei[i][index].second--;
if(wei[i][index].second == 0)index++;
}
}
cout << dp[s];
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
solve();
return 0;
}
/*
Use for suggestion:
{
while
}
{
nullptr
sync_with_stdio
false
return
to_string
length
}
{
push_back
pop_back
sort
reverse
begin
end
distance
assign
}
{
insert
erase
lower_bound
upper_bound
binary_search
}
{
first
second
swap
}
{
double
float
unsigned
}
{
unordered_map
pair
vector
deque
string
auto
cout
}
*/
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'void solve()':
knapsack.cpp:34:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | if(index >= wei[i].size())break;
| ~~~~~~^~~~~~~~~~~~~~~~
# | 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... |