Advertisement
Mehulcoder

Rupeek2021-C

Nov 9th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. /*
  2. Author: Mehul Chaturvedi
  3. Talent is overrated
  4. */
  5.  
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8.  
  9. void solve() {
  10.     int n, sum = 0;
  11.     cin >> n;
  12.     vector<pair<int, string>> v;
  13.     vector<int> pre(n, 0);
  14.  
  15.     for (int i = 0; i < n; ++i) {
  16.         string s; cin >> s;
  17.         int wt = 0;
  18.         for (auto &elem : s) wt += elem - 'a' + 1;
  19.         v.push_back({wt, s});
  20.         sum += wt;
  21.     }
  22.  
  23.     sort(v.begin(), v.end());
  24.     for (int i = 0; i < n; ++i) {
  25.         pre[i] = v[i].first;
  26.         if (i != 0) pre[i] += pre[i - 1];
  27.     }
  28.  
  29.  
  30.     int ans = INT_MAX;
  31.     string anss = "";
  32.  
  33.     //Remove last element
  34.     for (int i = 0; i < n - 1; ++i) {
  35.         int cost = sum - v[n - 1].first + v[i].first * (2 * i - n);
  36.         if (i != 0) cost -= 2 * pre[i - 1];
  37.  
  38.         if (cost < ans) anss = v[i].second;
  39.         ans = min(ans, cost);
  40.     }
  41.  
  42.     //Remove first element
  43.     for (int i = 1; i < n; ++i) {
  44.         int cost = sum + v[0].first + v[i].first * (2 * i - n);
  45.         if (i != 0) cost -= 2 * pre[i - 1];
  46.  
  47.         if (cost < ans) anss = v[i].second;
  48.         ans = min(ans, cost);
  49.     }
  50.  
  51.     cout << anss << '\n';
  52.     return;
  53. }
  54.  
  55. int main(int argc , char ** argv) {
  56.     int t = 1;
  57.     cin >> t;
  58.     while (t--) {
  59.         solve();
  60.     }
  61.  
  62.     return 0 ;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement