-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtankfill.c
More file actions
39 lines (36 loc) · 881 Bytes
/
tankfill.c
File metadata and controls
39 lines (36 loc) · 881 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 <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++) scanf("%d",&a[i]);
int max = 0;
int left[n];
for(int i=0;i<n;i++){
if(max<a[i]) max = a[i];
left[i] = max;
}
max = 0;
int right[n];
for(int i=n-1;i>=0;i--){
if(max<a[i]) max = a[i];
right[i] = max;
}
int total = 0;
for(int i=0;i<n;i++){
int m;
if(left[i]<=right[i]) m = left[i];
else m = right[i];
int waterati = m - a[i];
total+=waterati;
}
printf("%d\n",total);
}
return 0;
}