492C - Vanya and Exams - CodeForces Solution


greedy sortings *1400

Please click on ads to support us..

Python Code:

x = [int(i) for i in input().split()]
n = x[0]
r = x[1]
goal = x[2] * n
exams = []

total = 0
for i in range(n):
  a, b = [int(i) for i in input().split()]
  total += a
  exams.append((b, a))

if total >= goal:
  print(0)
else:
  exams.sort()
  ans = 0   for exam in exams:
    avail = r - exam[1]
    if avail > 0:
      if avail > goal - total:
                ans += (goal-total) * exam[0]
        break
      else:
                ans += avail * exam[0]
        total += avail
  print(ans)


C++ Code:

#include <bits/stdc++.h>
#include<unordered_map>

using namespace std;
#define all(v) ((v).begin()), ((v).end())
#define sz(v) (int)v.size()
#define endl "\n"
typedef long long ll;
constexpr auto EPS = 1e-9;
const long long mod = (long long) 1e9 + 7;
const int N = 1e7 + 2;
const double pi = acos(-1);
const int oo = 0x3f3f3f3f;
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};

void Fast() {
    std::ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

int main() {
    Fast();
    ll n, r, avg;
    cin >> n >> r >> avg;
    vector<pair<ll, ll>> grades(n);
    ll sum = 0;
    for (auto &it: grades) {
        cin >> it.second >> it.first;
        sum += it.second;
    }
    sort(all(grades));
    ll cnt = 0;
    ll need = avg * n;
    for (int i = 0; i < n; i++) {
        if (grades[i].second < r and sum < need) {
            if (r - grades[i].second < need - sum) {
                cnt += (r - grades[i].second) * grades[i].first;
                sum += r - grades[i].second;
            } else {
                cnt += (need - sum) * grades[i].first;
                sum += (need - sum);
            }
        }

    }

    cout << cnt << endl;
}


Comments

Submit
0 Comments
More Questions

1613A - Long Comparison
1624B - Make AP
660B - Seating On Bus
405A - Gravity Flip
499B - Lecture
709A - Juicer
1358C - Celex Update
1466B - Last minute enhancements
450B - Jzzhu and Sequences
1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating
270A - Fancy Fence
181A - Series of Crimes
1638A - Reverse
1654C - Alice and the Cake
369A - Valera and Plates
1626A - Equidistant Letters
977D - Divide by three multiply by two
1654B - Prefix Removals
1654A - Maximum Cake Tastiness
1649A - Game
139A - Petr and Book
1612A - Distance
520A - Pangram
124A - The number of positions
1041A - Heist
901A - Hashing Trees
1283A - Minutes Before the New Year
1654D - Potion Brewing Class