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 <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <climits>
#include <cstdlib>
#include <algorithm> // transform
#include <array> // array
#include <forward_list> // forward_list
#include <iterator> // inserter, front_inserter, end
#include <map> // map
#include <set> // set
#include <string> // string
#include <tuple> // tuple, make_tuple
#include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
#include <unordered_map> // unordered_map
#include <utility> // pair, declval
#include <valarray> // valarray
#include <unordered_set>
#include <queue>
#include <sstream>
#include <bitset>
using namespace std;
#define int long long
#define double long double
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v)
{
string sep;
for (const T &x : v)
os << sep << x, sep = " ";
return os;
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T)
{
cerr << ' ' << H;
dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
const int MOD = 1e9 + 7;
const int INF = 1e18;
const double EPS = 1e-9;
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
void solve();
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while(t--){
solve();
}
return 0;
}
bool cmp(pair<int, int> &a, pair<int, int> &b){
if(a.first != b.first) return a.first < b.first;
return a.second < b.second;
}
void solve(){
int s, n;
cin >> s >> n;
map<int, vector<pair<int, int>>> mp;
map<int, vector<int>> a;
for(int i=0; i<n; i++){
int v, w, x;
cin >> v >> w >> x;
mp[w].push_back({v, x});
}
for(auto x: mp) {
sort(mp[x.first].begin(), mp[x.first].end(), cmp);
}
for(auto x: mp){
while(x.first * a[x.first].size() < s and x.second.size() and x.second[x.second.size() - 1].second){
a[x.first].push_back(x.second[x.second.size() - 1].first);
x.second[x.second.size() - 1].second--;
if(x.second[x.second.size() - 1].second == 0) x.second.pop_back();
}
}
vector<vector<int>> dp(s+1, vector<int>(s+1));
for(int i=1; i<=s; i++){
for(int x=1; x<=s; x++){
dp[i][x] = dp[i-1][x];
int sum = 0;
for(int j=0; j<a[i].size(); j++){
if(x < (j+1)*i ) break;
sum += a[i][j];
dp[i][x] = max(dp[i][x], sum + dp[i-1][x-(j+1)*i]);
}
}
}
int ans = 0;
for(int i=1; i<=s; i++) {
ans = max(ans, dp[s][i]);
}
cout << ans << endl;
}
Compilation message (stderr)
knapsack.cpp: In function 'void solve()':
knapsack.cpp:90:43: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'long long int' [-Wsign-compare]
90 | while(x.first * a[x.first].size() < s and x.second.size() and x.second[x.second.size() - 1].second){
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
knapsack.cpp:102:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int j=0; j<a[i].size(); j++){
| ~^~~~~~~~~~~~
# | 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... |