-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP1.cpp
More file actions
39 lines (37 loc) · 894 Bytes
/
Copy pathP1.cpp
File metadata and controls
39 lines (37 loc) · 894 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
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
for(int x = 0; x < T; x++){
int n, k;
cin >> n >> k;
vector<int> height(n);
for (int i = 0; i < n; i++){
cin >> height[i];
}
sort(height.rbegin(), height.rend());
int l = 0;
while(k>0){
k = k - height[l];
if(k > 0){
height[l]--;
}
if (height[l+1] > height[l] && l!=n-1){
l++;
}
else if (l!=0 && height[l-1] >= height[l]){
l--;
}
else if(l == n-1){
l = 0;
}
}
int ans;
if (height[0] < 0){
height[0] = 0;
}
cout << height[0] <<endl;
}
return 0;
}