Submission #76475750


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define int long long

const int inf = 1e9+7;	 


void solve() {
	int n,k,m; 
	cin >> n >> k >> m;
	vector<pair<int,int>> values(n+1);
	for(int i=1; i<n+1; i++) {
		int temp_c, temp_v;
		cin >> temp_c >> temp_v;
		values[i]={temp_c, temp_v};
	}
	sort(values.begin()+1, values.end(), [&](pair<int,int> a, pair<int,int> b) {
		if(a.second!=b.second) {
			return a.second > b.second;   //just sort according to values first.
		}
		return a.first > b.first;    //prevents same color, same values -> gives variety.
	});
	int max_v=0;
	int total_picked=0;
	//first ensure variety - take max values from each color:
	int unique_color_cnt=0;
	vector<bool> colors_included(n+1,false);
	vector<bool> valid(n+1,true);   //a valid index from the sorted array values will be included for max_v calc once the bare minimum variety is confirmed.
	for(int i=1; i<n+1; i++) {
		if(!colors_included[values[i].first] && unique_color_cnt<m)  {
			max_v+=values[i].second;
			colors_included[values[i].first] = true;
			valid[i]=false;
			unique_color_cnt++;
			total_picked++;
			// cout << "new color chosen: " << values[i].first << "value chosen: " << values[i].second <<  endl;
		}
	}
	// cout << total_picked << endl;
	//now add the remaining overall max values until k gems are there - from the valid indices ofc:
	for(int i=1; i<n+1; i++) {
		if(valid[i] && total_picked<k) {
			// cout << "total picked: " << total_picked << endl;
			max_v+=values[i].second;
			total_picked++;
		}	
	}
	cout << max_v; 
}


signed main() {

	#ifdef local
	auto _clock_start = chrono::high_resolution_clock::now();
	#endif

	ios::sync_with_stdio(false);
	cin.tie(0); 
	int tc = 1;
	// cin >> tc;
	while(tc--) {
		solve();
		cout << nl;
	}

	#ifdef local
	cerr << "Executed in " << chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - _clock_start).count() << "ms." << nl;
	#endif

	return 0;
}

Submission Info

Submission Time
Task C - Variety
User parthkoul_
Language C++23 (GCC 15.2.0)
Score 300
Code Size 2027 Byte
Status AC
Exec Time 31 ms
Memory 6524 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 3
AC × 23
Set Name Test Cases
Sample sample-01.txt, sample-02.txt, sample-03.txt
All 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, sample-01.txt, sample-02.txt, sample-03.txt
Case Name Status Exec Time Memory
04.txt AC 2 ms 3592 KiB
05.txt AC 18 ms 6460 KiB
06.txt AC 18 ms 6316 KiB
07.txt AC 21 ms 6428 KiB
08.txt AC 1 ms 3452 KiB
09.txt AC 1 ms 3712 KiB
10.txt AC 1 ms 3580 KiB
11.txt AC 12 ms 4676 KiB
12.txt AC 29 ms 6300 KiB
13.txt AC 31 ms 6508 KiB
14.txt AC 31 ms 6460 KiB
15.txt AC 30 ms 6436 KiB
16.txt AC 30 ms 6468 KiB
17.txt AC 31 ms 6460 KiB
18.txt AC 29 ms 6368 KiB
19.txt AC 31 ms 6368 KiB
20.txt AC 31 ms 6428 KiB
21.txt AC 30 ms 6508 KiB
22.txt AC 28 ms 6436 KiB
23.txt AC 30 ms 6524 KiB
sample-01.txt AC 1 ms 3488 KiB
sample-02.txt AC 1 ms 3432 KiB
sample-03.txt AC 1 ms 3648 KiB