Official

F - Flavors Editorial by en_translator


Divide into the following cases.

  • Eat two cups of ice cream of the same flavor.
    • For each flavor, it is sufficient to inspect the case where the two ice creams with the greatest deliciousnesses are chosen.
  • Eat two cups of ice cream of the different flavors.
    • First, we can assume that only the ice cream with the greatest deliciousness within the same flavor are chosen. Among the remaining ice cream, it is sufficient to choose the two cups with the greatest deliciousnesses.
#include<bits/stdc++.h>

using namespace std;

int main(){
  int n;
  cin >> n;
  vector<vector<int>> bk(n+1);
  for(int i=0;i<n;i++){
    int f,s;
    cin >> f >> s;
    bk[f].push_back(s);
  }

  int res=0;
  vector<int> best;
  for(int i=1;i<=n;i++){
    sort(bk[i].begin(),bk[i].end());
    reverse(bk[i].begin(),bk[i].end());
    if(bk[i].size()>=2){
      res=max(res,bk[i][0]+bk[i][1]/2);
    }
    if(bk[i].size()>=1){
      best.push_back(bk[i][0]);
    }
  }
  sort(best.begin(),best.end());
  reverse(best.begin(),best.end());
  if(best.size()>=2){res=max(res,best[0]+best[1]);}
  cout << res << "\n";
  return 0;
}

posted:
last update: