Submission #863113

# Submission time Handle Problem Language Result Execution time Memory
863113 2023-10-19T15:32:27 Z sano Knapsack (NOI18_knapsack) C++14
100 / 100
55 ms 4824 KB
#include <iostream>
#include <string>
#include <string.h>
#include <fstream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <queue>
#include<map>
#include <stack>
#include <unordered_map>
#include <set>
#define ll long long
#define For(i, n) for(int i = 0; i < n; i++)
#define pb push_back
#define vec vector 
#define ff first
#define ss second
#define pairs pair<int, int>
#define NEK 1000000000
#define PRI 1000000007
using namespace std;

struct tr {
	int a;
	int b;
	int c;
};

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n, s;
	cin >> s >> n;
	map<int, vec<pairs>> p;
	For(i, n) {
		int v, w, m;
		cin >> v >> w >> m;
		if (w <= s && m > 0) {
			p[w].push_back({ v, m });
		}
	}

	vec<vec<int>> b(2, vec<int>(s + 1, 0));
	int t2 = 1;
	for (auto e : p) {
		int now = t2 % 2;
		int last = 1 - now;
		auto a = e.first;
		vec<pairs> u = e.second;
		sort(u.begin(), u.end(), greater<pairs>());
		for (int i = 0; i < s + 1; i++) {
			b[now][i] = b[last][i];
			int poz = 0;
			int pen = 0;
			int kop = 0;
			int tkop = 0;
			while ((kop + 1) * a <= i && poz < u.size()) {
				kop++;
				pen = pen + u[poz].first;
				b[now][i] = max(b[now][i], b[last][i - kop * a] + pen);
				tkop++;
				if (tkop == u[poz].ss) {
					tkop = 0;
					poz++;
				}
			}
		}
		t2++;
	}
	cout << b[(t2-1)%2][s] << endl;
	return 0;
}

Compilation message

knapsack.cpp: In function 'int main()':
knapsack.cpp:58:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |    while ((kop + 1) * a <= i && poz < u.size()) {
      |                                 ~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 2 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 3 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 2 ms 496 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 2 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 3 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 2 ms 496 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 23 ms 3800 KB Output is correct
36 Correct 26 ms 4056 KB Output is correct
37 Correct 25 ms 4012 KB Output is correct
38 Correct 23 ms 3020 KB Output is correct
39 Correct 30 ms 3720 KB Output is correct
40 Correct 55 ms 3928 KB Output is correct
41 Correct 43 ms 3052 KB Output is correct
42 Correct 53 ms 2984 KB Output is correct
43 Correct 51 ms 3156 KB Output is correct
44 Correct 52 ms 3156 KB Output is correct
45 Correct 30 ms 3668 KB Output is correct
46 Correct 27 ms 4824 KB Output is correct
47 Correct 32 ms 3668 KB Output is correct
48 Correct 42 ms 3768 KB Output is correct
49 Correct 26 ms 3412 KB Output is correct
50 Correct 38 ms 3340 KB Output is correct