-
Notifications
You must be signed in to change notification settings - Fork 0
/
URI_2715.cpp
70 lines (56 loc) · 1.86 KB
/
URI_2715.cpp
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
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <vector>
#include <cmath>
#include <climits>
int main()
{
int n{}, x{};
int64_t sumRangel{}, sumGugu{}, diff{LLONG_MAX};
std::vector<int> difficulty;
while(std::cin>>n)
{
difficulty.reserve(n);
for(int i = 0; i < n; ++i)
{
std::cin>>x;
if(i < std::ceil(n / 2.f))
sumRangel += x;
else
sumGugu += x;
difficulty[i] = x;
}
if(sumRangel > sumGugu)
{
int i = std::ceil(n / 2.f) - 1;
while(!(sumRangel - difficulty[i] < sumGugu + difficulty[i]) && i >= 0)
{
sumRangel -= difficulty[i];
sumGugu += difficulty[i];
i--;
}
if(i) // Exited earlier, need to check if we left behind the optimal solution
{
diff = std::min(std::abs(sumRangel - sumGugu), std::abs((sumRangel - difficulty[i]) - (sumGugu + difficulty[i])));
}
}
else
if(sumRangel < sumGugu)
{
int i = std::ceil(n / 2.f);
while(!(sumRangel + difficulty[i] > sumGugu - difficulty[i]) && i < n)
{
sumGugu -= difficulty[i];
sumRangel += difficulty[i];
i++;
}
if(i < n) // Exited earlier, need to check if we left behind the optimal solution
{
diff = std::min(std::abs(sumRangel - sumGugu), std::abs((sumRangel + difficulty[i]) - (sumGugu - difficulty[i])));
}
}
std::cout<<std::min(diff, std::abs(sumRangel - sumGugu))<<'\n';
difficulty.clear();
sumRangel = sumGugu = 0;
}
return 0;
}