Untitled
user_5668965
c_cpp
a year ago
1.4 kB
22
Indexable
#include <bits/stdc++.h>
#define ll long long
#define db(x) cout << (#x) << " = " << x <<endl;
#define pb push_back
#define mt make_tuple
#define F first
#define S second
using namespace std;
bool comp(pair <ll ,ll> &a, pair <ll , ll> &b){
if(a.F==b.F) return a.S < b.S;
return a.F > b.F;
}
int main(){
ll n;
vector <ll> bt , al , b , non;
cin >> n;
while(n--){
string s;
ll a;
cin >> s >> a;
if(s=="00") non.pb(a);
if(s=="10") b.pb(a);
if(s=="11") bt.pb(a);
if(s=="01") al.pb(a);
}
sort(al.begin(),al.end(),greater<ll>());
sort(b.begin(),b.end(),greater<ll>());
sort(bt.begin(),bt.end(),greater<ll>());
sort(non.begin(),non.end(),greater<ll>());
if(al.size()+bt.size()==0 and b.size()+al.size()==0){
cout << 0 ;
return 0;
}
ll ans = 0 , s1 = 0 , s2 = 0 , tot = 0;
for(ll i = 0;i<bt.size();i++){
ans += bt[i];
tot++;
s1++;
s2++;
}
for(ll i = 0;i<min(al.size(),b.size());i++){
ans = ans + al[i];
ans = ans + b[i];
tot += 2;
s1++;
s2++;
}
ll left = 2*min(s1,s2) - tot;
for(ll i = min(al.size(),b.size());i<al.size() or i<b.size();i++){
if(i<al.size()) non.pb(al[i]);
if(i<b.size()) non.pb(b[i]);
}
sort(non.begin(),non.end(),greater<ll>());
for(ll i=0;i<min(left,(ll)non.size());i++) ans += non[i];
cout << ans << endl;
}Editor is loading...
Leave a Comment