Submission #486238

#TimeUsernameProblemLanguageResultExecution timeMemory
486238joshualiu555Knapsack (NOI18_knapsack)C++14
12 / 100
0 ms204 KiB
/* _____ _ _ / ____| | | | | | | __ _ __ __ _ ___ ___ _ _ _ __ ___ | |_ __ _| |_ ___ | | |_ | '__/ _` / __/ __| | | | '_ \ / _ \| __/ _` | __/ _ \ | |__| | | | (_| \__ \__ \ |_| | |_) | (_) | || (_| | || (_) | \_____|_| \__,_|___/___/\__, | .__/ \___/ \__\__,_|\__\___/ __/ | | |___/|_| */ #include <fstream> #include <iostream> #include <iomanip> #include <algorithm> #include <numeric> #include <utility> #include <array> #include <vector> #include <set> #include <map> #include <stack> #include <queue> #include <cmath> #include <cstring> #include <climits> #include <bitset> #include <string> #include <sstream> using namespace std; #define FOR(i, x, s) for (int i = x; i < s; i++) #define TRAV(it, x) for (auto it = x.begin(); it != x.end(); it++) using ll = long long; const int INF = 2e5 + 5; const int MOD = 1e9 + 7; // DURL const int dx[4] = {1, -1, 0, 0}; const int dy[4] = {0, 0, 1, -1}; ll S, N; map<ll, vector<pair<ll, ll>>> weight_group; ll dp[2005]; int main() { std::ios_base::sync_with_stdio(false); cin.tie(0); // ifstream cin(".in"); // ofstream cout(".out"); memset(dp, -1, sizeof(dp)); cin >> S >> N; for (ll i = 0; i < N; i++) { ll V, W, K; cin >> V >> W >> K; weight_group[W].push_back(make_pair(V, K)); } dp[0] = 0; for (auto it = weight_group.begin(); it != weight_group.end(); it++) { ll weight = it -> first; // first = value, second = amount vector<pair<ll, ll>> items = it -> second; for (ll i = S; i >= 0; i--) { if (dp[i] == -1) continue; ll current = i; ll id = 0; ll left = items[id].second; while (current <= S) { if (id == (ll)items.size()) break; dp[current + weight] = max(dp[current + weight], dp[current] + items[id].first); left--; if (left == 0) { id++; left = items[id].second; } current += weight; } } } cout << *max_element(dp, dp + S + 1); return 0; } /* * */ //
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...