-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf_round890_div2_C.java
46 lines (38 loc) · 1.26 KB
/
cf_round890_div2_C.java
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
41
42
43
44
45
46
import java.util.*;
public class cf_round890_div2_C {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt(); // Number of test cases
for (int i = 0; i < t; i++) {
int n = scanner.nextInt(); // Length of the array
int k = scanner.nextInt(); // Maximum number of operations
int[] a = new int[n];
for (int j = 0; j < n; j++) {
a[j] = scanner.nextInt(); // Elements of the array
}
int result = getMaxValue(a, k);
System.out.println(result);
}
}
public static int getMaxValue(int[] a, int k) {
int max = Integer.MIN_VALUE;
for(int i = 0;i<a.length-1 && k>0; i++){
if(a[i] < a[i+1])
{
int diff = a[i+1] - a[i];
if(k>=diff * (i+1)){
a[i] += diff;
k -= diff*(i+1);
}
else{
a[i] += k/(i+1);
break;
}
}
}
for(int p : a){
max = Math.max(p, max);
}
return max;
}
}