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>
#define ll long long
#define endl '\n'
#define rep(i,a,b) for(int i=a; i<=b; ++i)
#define rev(i,b,a) for(int i=b; i>=a; --i)
#define pii pair<ll,ll>
using namespace std;
const int maxN = 1e6 + 7;
const ll inf = 1e9 + 7;
const ll moreinf = 1e16 + 7;
struct data{
int V,W,K;
}a[maxN];
int n,s;
ll f[2002][2002];
vector<pii> item[2002];
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> s >> n;
rep(i,1,n) cin >> a[i].V >> a[i].W >> a[i].K;
rep(i,1,n){
item[a[i].W].emplace_back(a[i].V, a[i].K);
}
rep(i,1,s){
sort(begin(item[i]),end(item[i]), [](pii a, pii b)->bool{
return a.first > b.first;
});
}
// rep(i,1,s){
// if(item[i].empty()) continue;
// for(auto it:item[i]) cout << it.first << " " << it.second << endl;
// // cout << endl;
// }
vector<pii> use_item;
rep(weight,1,s){
int cnt = 0;
for(auto it:item[weight]){
rep(i,1,it.second){
++cnt;
use_item.emplace_back(it.first,weight);
}
if(cnt>=s/weight) break;
}
}
rep(i,0,use_item.size()-1){
rep(j,0,s){
f[i][j] = f[i-1][j];
if(j>=use_item[i].second) f[i][j] = max(f[i][j], f[i-1][j-use_item[i].second] + use_item[i].first);
}
}
cout << f[(int)use_item.size()-1][s];
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:53:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | #define rep(i,a,b) for(int i=a; i<=b; ++i)
......
98 | rep(i,0,use_item.size()-1){
| ~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:98:5: note: in expansion of macro 'rep'
98 | rep(i,0,use_item.size()-1){
| ^~~
# | 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... |