#C5164. Minimum Absolute Difference in Circular Pearl Groups

    ID: 48783 Type: Default 1000ms 256MiB

Minimum Absolute Difference in Circular Pearl Groups

Minimum Absolute Difference in Circular Pearl Groups

In this problem, you are given a circular arrangement of pearls, each having an integer value. The pearls are arranged along a circle, and you must split them into two contiguous non-empty groups. Due to the circular configuration, one of the groups may wrap around from the end of the list to the beginning. Let (S_1) and (S_2) be the sums of the pearls in the two groups respectively. Your task is to determine the minimum possible absolute difference (|S_1 - S_2|) between these two sums.

Example: If the pearls are [1, 2, 3, 4] arranged in a circle, one optimal partition is [1, 2, 3] and [4], resulting in an absolute difference of (|6 - 4| = 2). However, a better partition such as [1, 2] and [3, 4] gives an absolute difference of (|3 - 7| = 4). (Note: The provided sample cases illustrate different scenarios.)

inputFormat

The input is provided via standard input. The first line contains an integer (n), the number of pearls. The second line contains (n) space-separated integers representing the values of the pearls in the order they appear on the circle.

outputFormat

Output a single integer to standard output: the minimum possible absolute difference between the sums of the two contiguous groups into which the pearls can be split.## sample

4
1 2 3 4
0