-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.cpp
More file actions
40 lines (35 loc) · 974 Bytes
/
Copy pathC.cpp
File metadata and controls
40 lines (35 loc) · 974 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
long long k; // Only here to handle large inputs, but results will be in int
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int left = 1, right = n + k, answer = 0;
while (left <= right) {
int mid = (left + right) / 2;
long long total_needed = 0;
for (int i = 0; i < n; ++i) {
if (a[i] < mid) {
total_needed += (mid - a[i]);
}
}
if (total_needed <= k) {
answer = mid; // mid is possible
left = mid + 1;
} else {
right = mid - 1;
}
}
cout << answer << endl;
}
return 0;
}