-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCANDYDIST.cpp
More file actions
28 lines (27 loc) · 834 Bytes
/
CANDYDIST.cpp
File metadata and controls
28 lines (27 loc) · 834 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
#include<stdio.h>
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m,p=0,minn=10000007,minp=10000007;
scanf("%d%d",&n,&m);
int a,b,c=0;
scanf("%d",&a);
for(int i=0;i<m-1;i++){
scanf("%d",&b);
if(a==b)p--;
else{
p+=(b-a);
c-=(b-a);
}
//printf("%d\n",p);
if(c<minn)minn=c;
if(p<minp)minp=p;
a=b;
}
//minp=--p;
n+=minn+minp;
if(n<0)printf("-1\n");
else printf("%d\n",-p);
}
}