-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2715.c
52 lines (45 loc) · 1.07 KB
/
2715.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
long long min(long long a, long long b)
{
return a < b ? a : b;
}
int main()
{
int N;
while (scanf("%d", &N) != EOF)
{
long long *somas = (long long *)malloc(N * sizeof(long long));
scanf("%lld", &somas[0]);
for (int i = 1; i < N; ++i)
{
scanf("%lld", &somas[i]);
somas[i] += somas[i - 1];
}
int inicio = 0, fim = N;
long long resposta = somas[N - 1];
while (inicio < fim)
{
int meio = (inicio + fim) / 2;
long long rangel = somas[meio];
long long gugu = somas[N - 1] - rangel;
resposta = min(resposta, llabs(rangel - gugu));
if (rangel == gugu)
{
break;
}
else if (rangel < gugu)
{
inicio = meio + 1;
}
else
{
fim = meio;
}
}
printf("%lld\n", resposta);
free(somas);
}
return 0;
}