Submission #1370506

#TimeUsernameProblemLanguageResultExecution timeMemory
1370506marcogambaroAsteroid Mining (CCO25_day1problem1)C++20
8 / 25
111 ms17528 KiB
#include <bits/stdc++.h>
#include <cassert>
using namespace std;
mt19937 timmy_loves_gambling(73);
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define readv(v) do { for(auto &i: v) cin >> i; } while(0)
#define _ << " " <<
#define lf "\n"
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define fi first
#define se second
template<typename... Args>
using vec = vector<Args...>;
#ifndef MARCO
bool dbg = 0;
#define infor(str, ...)
#define infof(str, ...)
#else
bool dbg = 1;
#define infor(str, ...) do { print(stderr, str, ##__VA_ARGS__); } while(0)
#define infof(str, ...) do { println(stderr, str, ##__VA_ARGS__); } while(0)
#endif


signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);   
    
	int N; ll M;
	cin >> N >> M;

	vec<pll> V(N);
	for(auto &[v, w]: V) {
		cin >> v >> w;
	}

	sort(all(V), [&](pll i, pll j){
		if(i.se != j.se) return i.se < j.se;
		return i.fi > j.fi;
	});

	vec<vec<ll>> S;
	vec<ll> W;

	ll pw = -1;
	for(int i = 0; i < N; i++) {
		auto &[v, w] = V[i];

		if(pw == w) S.back().emplace_back(v);
		else {
			if(pw != -1) W.push_back(pw);
			S.push_back({v});
			pw = w;
		}
	}
	W.push_back(pw);

	M = M/W[0] * W[0];
	ll ans = 0;

	for(int i = 1; i < W.size(); i++) {
		for(int j=0; j<M%W[i]/W[i-1]; j++)
			ans += S[i-1][j];

		vec<ll> Sn;
		ll v = 0;
		int p = 0;
		ll d = 0;

		for(int j=M%W[i]/W[i-1]; j<S[i-1].size(); j++) {
			v += S[i-1][j];	
			d++;

			if(d == W[i]/W[i-1]) {
				while(p < S[i].size() && S[i][p] > v) 
					Sn.push_back(S[i][p++]);
				Sn.push_back(v);
				v = 0;
				d = 0;
			}
		}

		while(p < S[i].size()) 
			Sn.push_back(S[i][p++]);
	
		S[i].swap(Sn);

		M = M/W[i] * W[i];
	}

	for(int j=0; j<min(M/W.back(), (ll)S.back().size()); j++)
		ans += S.back()[j];

	cout << ans << lf;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...