We have N weights indexed 1 to N. The mass of the weight indexed i is W_i.
We will divide these weights into two groups: the weights with indices not greater than T, and those with indices greater than T, for some integer 1 \leq T < N. Let S_1 be the sum of the masses of the weights in the former group, and S_2 be the sum of the masses of the weights in the latter group.
Consider all possible such divisions and find the minimum possible absolute difference of S_1 and S_2.
Constraints
2 \leq N \leq 100
1 \leq W_i \leq 100
All values in
input are integers.
Input
Input is given from Standard Input in the following format:
N
W_1 W_2 ... W_{N-1} W_N
Output
Print the minimum possible absolute difference of S_1 and S_2.
Examples
Input
3
1 2 3
Output
0
Input
4
1 3 1 1
Output
2
Input
8
27 23 76 2 3 5 62 52
Output
2
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to parse the problem correctly. "We have N weights indexed 1 to N. The mass of the weight indexed i is W_i. We will divide these weights into two groups: the weights with indices not greater than T, and those with indices greater than T, for some integer 1 ≤ T < N. Let S1 be the sum of the masses of the weights in the former group, and S2 be the sum of the masses of the weights in the latter group. Consider all possible such divisions and find the minimum possible absolute difference of S1 and S2."
So we must choose a split point T between 1 and N-1, i.e., the first group: indices 1..T, second group: indices T+1..N. Then compute difference |sum(1..T) - sum(T+1..N)|, and find minimum across T.
This is straightforward: compute prefix sums, then iterate T from 1 to N-1, compute |prefix[T] - (total - prefix[T])|, keep minimum.
Constraints are small (N up to 100). So trivial.
But wait: The problem statement might be from AtCoder typical problem "Split" or "Coins"? Actually there is known problem "B - Balance" from AtCoder ABC 129 B? But there it's similar: N weights, split into two groups. But there the groups are left and right based on index. Yes. So it's exactly that.
So solution: read N, then array W. Compute total sum. Initialize prefix = 0. For i from 0 to N-2 (i.e., T=1..N-1), add W[i] to prefix, then diff = abs(prefix - (total - prefix)), update ans = min(ans, diff). Print ans.
Edge cases: N=2, T can be 1 only.
Examples:
3, [1,2,3]: total=6. T=1: prefix=1, diff=|1-5|=4; T=2: prefix=3, diff=|3-3|=0 => ans 0.
4, [1,3,1,1]: total=6. T=1: |1-5|=4; T=2:|4-2|=2; T=3:|5-1|=4 => ans 2.
8, given answer 2.
But let's verify with the example 8: weights: 27 23 76 2 3 5 62 52. Total sum? 27+23=50, +76=126, +2=128, +3=131, +5=136, +62=198, +52=250. So total=250. Now prefixes: T=1: 27, diff=|27-223|=196; T=2: 50, |50-200|=150; T=3: 126, |126-124|=2; T=4: 128, |128-122|=6; T=5:131, |131-119|=12; T=6:136, |136-114|=22; T=7:198, |198-52|=146. So min=2, matches.
Thus solution is straightforward.
We need to write code accordingly.
But the instruction: "We have N weights indexed 1 to N... All values in input are integers." So we can just implement.
But maybe there is a twist? No.
Thus final answer: python code.
But since the user asked "We have N weights ... find the minimum possible absolute difference of S1 and S2." and gave examples. They likely expect a solution.
Given the ChatGPT style: "Print the minimum possible absolute difference". So we need to output an integer.
Will write code.
Also consider using sys.stdin for reading.
We'll produce a clean solution.
Now, as per instructions: "You are ChatGPT ...", we should respond with the final answer.
But we need to produce code? The user did not explicitly ask for code; they gave a problem statement. Usually in these contexts, they expect a solution. However, the prompt is: "We have N weights ... find the minimum possible absolute difference ...". It looks like they are giving a problem, and we need to provide an answer. The final answer might be the output for the specific input? Actually they provided examples but didn't ask for code. The user likely wants a solution algorithm or code? However, typical format: "We have ... Input ... Output ..." is a problem statement, and they want the solution (the code). The conversation might be from a coding competition where you need to produce code. As ChatGPT, we can provide explanation and then code.
But the instruction: "Print the minimum possible absolute difference ..." suggests they want the answer for a given input. However, the input is not specified beyond the examples. Actually the user gave examples, but the actual input is not provided. The typical structure in AtCoder problems: they present the problem statement and expect a program. In such cases, the user might be asking for the solution (code). They didn't give a specific test case; they gave examples. So they likely want a general solution.
Thus, I'll produce a Python solution.
Now, implement:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
N = int(data[0])
W = list(map(int, data[1:1+N]))
total = sum(W)
prefix = 0
ans = total # max difference
for i in range(N-1):
prefix += W[i]
diff = abs(prefix - (total - prefix))
if diff < ans:
ans = diff
print(ans)
if name == "main":
solve()
That's it.
But we might also consider that the input might have extra spaces or newlines; it's fine.
Thus final response: Provide explanation and code.
Given the typical format, I'll output the code.
But we need to follow the "final" channel with just the answer? The instruction: "We have N weights ..." is a problem statement. As ChatGPT, we should produce the solution code. Usually, they'd say "write a program" etc. Here it's ambiguous. But it's safe to provide the code.
Also note that the problem is trivial, but we must ensure to handle N up to 100, so O(N) fine.
Therefore final answer.
I'll also note that this is from AtCoder ABC 129 B? Actually ABC 129 B is "Balance". So it's good.
I will produce answer in Python.
Thus final.
```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
w = list(map(int, data[1:1 + n]))
total = sum(w)
code
prefix = 0
ans = total # maximum possible difference
for i in range(n - 1):
prefix += w[i]
diff = abs(prefix - (total - prefix))
if diff < ans:
ans = diff
print(ans)