Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- int g, n, m; cin >> g >> n >> m;
- multiset<int> gr;
- for (int i = 0; i < g; ++i) {
- int p; cin >> p;
- _test += p;
- gr.insert(p);
- }
- int broj = 0;
- auto f = gr.begin();
- auto b = prev(gr.end());
- int fv = *f, bv = *b;
- while (true) {
- f = gr.begin();
- b = prev(gr.end());
- fv = *f;
- bv = *b;
- if (fv >= n && bv <= m) {
- break;
- }
- bool changed = false;
- if (fv < n) {
- changed = true;
- int chng = n - fv;
- bv = bv - chng;
- fv = n;
- broj += chng;
- gr.erase(f);
- gr.erase(b);
- gr.insert(fv);
- gr.insert(bv);
- // cout << "fv<n chng=" << chng << " | ";
- // int _s = 0;
- // for (auto it : gr) {
- // cout << it << " "; _s += it;
- // }
- // cout << "(" << _s << ")" << endl;
- }
- f = gr.begin();
- b = prev(gr.end());
- fv = *f;
- bv = *b;
- if (bv > m) {
- changed = true;
- int chng = bv - m;
- fv = fv + chng;
- bv = m;
- gr.erase(f);
- gr.erase(b);
- gr.insert(fv);
- gr.insert(bv);
- // cout << "bv>m chng=" << chng << " | ";
- // int _s = 0;
- // for (auto it : gr) {
- // cout << it << " "; _s += it;
- // }
- // cout << "(" << _s << ")" << endl;
- }
- if (!changed) {
- broj = -1;
- break;
- }
- }
- cout << broj << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement