#P10429. [蓝桥杯 2024 省 B] 拔河

[蓝桥杯 2024 省 B] 拔河

Problem Description

Xiaoming is a teacher at a school. There are nn students in his class, and the strength value of the ii-th student is aia_i. In his free time, Xiaoming decides to organize a tug-of-war contest in the class.

To make the two sides as evenly matched as possible, he needs to choose two teams from these nn students. The students in each team must have consecutive indices: $\{{a_{l_1}}, a_{l_1 + 1}, \dots, a_{r_1 - 1}, a_{r_1}\}$ and $\{{a_{l_2}}, a_{l_2 + 1}, \dots, a_{r_2 - 1}, a_{r_2}\}$, where l1r1<l2r2l_1 \le r_1 < l_2 \le r_2.

The two teams do not have to have the same number of students, but the sums of the strength values within the two teams should be as close as possible. Please compute the minimum possible difference between the sums of strength values among all valid ways to pick the two teams.

Input Format

The input has two lines.
The first line contains a positive integer nn.
The second line contains nn positive integers a1,a2,,ana_1, a_2, \dots, a_n.

Output Format

Output one line containing a non-negative integer, representing the minimum difference between the sums of strength values of the two teams.

5
10 9 8 12 14

1

Hint

Sample 1 Explanation

One optimal selection is:

Team 11: {a1,a2,a3}\{a_1, a_2, a_3\}, Team 22: {a4,a5}\{a_4, a_5\}. The sums of strength values are 10+9+8=2710 + 9 + 8 = 27 and 12+14=2612 + 14 = 26, so the difference is 2726=1|27 − 26| = 1.

Constraints

  • For 20%20\% of the testdata, n50n \leq 50.
  • For all testdata, it is guaranteed that 2n1032 \leq n \leq 10^3 and 1ai1091 \leq a_i \leq 10^9.

Translated by ChatGPT 5