Submission #887075

#TimeUsernameProblemLanguageResultExecution timeMemory
887075whoKnapsack (NOI18_knapsack)C++17
100 / 100
142 ms27264 KiB
#include <bits/stdc++.h>

using namespace std;

#define task "a"
#define etr "\n"
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pli pair<long long,int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define bg begin
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define lwb lower_bound
#define upb upper_bound
#define range(x, l, r) x+l, x+1+r
#define all(x) (x).bg(), (x).end()
#define compact(x) x.resize(unique(all(x)) - (x).bg())
#define sq(x) ((x)*(x))

auto start = chrono::high_resolution_clock::now();

void start_timer()
{
    start = chrono::high_resolution_clock::now();
}

ld elapsed()
{
    auto current = chrono::high_resolution_clock::now();
    ld duration = chrono::duration_cast<chrono::nanoseconds>(current - start).count();
    return duration / 1e9;
}
void freop()
{
	freopen(task".inp", "r", stdin);
	freopen(task".out", "w", stdout);
}

template<typename U, typename V> istream& operator >> (istream& in, pair<U, V>& p)
{
	in >> p.fi >> p.se;
	return in;
}

template<typename U, typename V> ostream& operator << (ostream& out, pair<U, V> p)
{
	out << p.fi << ' ' << p.se;
	return out;
}

const int N=2e3, M=1e5, mod=1e9+7;

typedef array<ll, 3> Item;

int n, s;
ll dp[N+5];
vector<Item> adj[N+5];

void process()
{
    cin >> s >> n;
    vector<Item> a;
    for (int i=1; i<=n; i++)
    {
        ll v, w, k;
        cin >> v >> w >> k;

        for (int j=1; j<=k; j<<=1)
        {
            if (w * j > s) break;
            adj[w*j].pb({v*j, w*j, k});
            k -= j;
        }

        if (w * k <= s) adj[w*k].pb({v*k, w*k, k});
    }

    for (int i=1; i<=s; i++)
    {
        sort(all(adj[i]), greater<Item>());
        for (int j=0; j<min((int)adj[i].size(), s/i); j++) a.pb(adj[i][j]);
    }

    n = a.size();

    for (int i=0; i<n; i++)
    {
        for (int j=s; j>=a[i][1]; j--) dp[j] = max(dp[j], dp[j - a[i][1]] + a[i][0]);
    }

    ll res = 0;
    for (int i=0; i<=s; i++) res = max(res, dp[i]);

    cout << res;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int t=1; //cin >> t;
	while (t--) process();

	return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'void freop()':
knapsack.cpp:41:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |  freopen(task".inp", "r", stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:42:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |  freopen(task".out", "w", stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...