-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprladdu.c
58 lines (56 loc) · 1.43 KB
/
prladdu.c
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
47
48
49
50
51
52
53
54
55
56
57
58
#include<stdio.h>
int main()
{
long long int t,n,h_in,d_in,dif,count,vil[100005],human[100005],dino[100005],i,h,d;
scanf("%lld",&t);
while(t--)
{
scanf("%lld",&n);
d_in=0;h_in=0;count=0,h=0;d=0;
for(i=0;i<n;i++)
{
scanf("%lld",&vil[i]);
if(vil[i]>0)
{
human[h_in]=i;
//printf("%lld",human[h_in]);
h_in++;
}
if(vil[i]<0)
{
dino[d_in]=i;
//printf("%lld",dino[d_in]);
d_in++;
vil[i]=-vil[i];
}
}
while(h<h_in && d<d_in)
{
if(vil[human[h]]>=vil[dino[d]])
{
vil[human[h]]=vil[human[h]]-vil[dino[d]];
dif=human[h]-dino[d];
if(dif<0)
dif=-dif;
dif=dif*vil[dino[d]];
count=count+dif;
vil[dino[d]]=0;
}
else
{
vil[dino[d]]=vil[dino[d]]-vil[human[h]];
dif=human[h]-dino[d];
if(dif<0)
dif=-(dif*vil[human[h]]);
count=count+dif;
vil[human[h]]=0;
}
if(vil[human[h]]==0)
h++;
if(vil[dino[d]]==0)
d++;
}
printf("%lld\n",count);
}
return 0;
}