Alice, Bob and Candies (Codeforces 1352D)
Tags: Implement, implementation

There are $$$n$$$ candies in a row, they are numbered from left to right from $$$1$$$ to $$$n$$$. The size of the $$$i$$$-th candy is $$$a_i$$$.Alice and Bob play an interesting and tasty game: they eat candy. Alice will eat candy from left to right, and Bob — from right to left. The game ends if all the candies are eaten.The process consists of moves. During a move, the player eats one or more sweets from her/his side (Alice eats from the left, Bob — from the right).Alice makes the first move. During the first move, she will eat $$$1$$$ candy (its size is $$$a_1$$$). Then, each successive move the players alternate — that is, Bob makes the second move, then Alice, then again Bob and so on.On each move, a player counts the total size of candies eaten during the current move. Once this number becomes strictly greater than the total size of candies eaten by the other player on their previous move, the current player stops eating and the move ends. In other words, on a move, a player eats the smallest possible number of candies such that the sum of the sizes of candies eaten on this move is strictly greater than the sum of the sizes of candies that the other player ate on the previous move. If there are not enough candies to make a move this way, then the player eats up all the remaining candies and the game ends.For example, if $$$n=11$$$ and $$$a=[3,1,4,1,5,9,2,6,5,3,5]$$$, then:  move 1: Alice eats one candy of size $$$3$$$ and the sequence of candies becomes $$$[1,4,1,5,9,2,6,5,3,5]$$$.  move 2: Alice ate $$$3$$$ on the previous move, which means Bob must eat $$$4$$$ or more. Bob eats one candy of size $$$5$$$ and the sequence of candies becomes $$$[1,4,1,5,9,2,6,5,3]$$$.  move 3: Bob ate $$$5$$$ on the previous move, which means Alice must eat $$$6$$$ or more. Alice eats three candies with the total size of $$$1+4+1=6$$$ and the sequence of candies becomes $$$[5,9,2,6,5,3]$$$.  move 4: Alice ate $$$6$$$ on the previous move, which means Bob must eat $$$7$$$ or more. Bob eats two candies with the total size of $$$3+5=8$$$ and the sequence of candies becomes $$$[5,9,2,6]$$$.  move 5: Bob ate $$$8$$$ on the previous move, which means Alice must eat $$$9$$$ or more. Alice eats two candies with the total size of $$$5+9=14$$$ and the sequence of candies becomes $$$[2,6]$$$.  move 6 (the last): Alice ate $$$14$$$ on the previous move, which means Bob must eat $$$15$$$ or more. It is impossible, so Bob eats the two remaining candies and the game ends. Print the number of moves in the game and two numbers:  $$$a$$$ — the total size of all sweets eaten by Alice during the game;  $$$b$$$ — the total size of all sweets eaten by Bob during the game. 

Input: The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of test cases in the input. The following are descriptions of the $$$t$$$ test cases.Each test case consists of two lines. The first line contains an integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of candies. The second line contains a sequence of integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 1000$$$) — the sizes of candies in the order they are arranged from left to right.It is guaranteed that the sum of the values of $$$n$$$ for all sets of input data in a test does not exceed $$$2\cdot10^5$$$.

Output: For each set of input data print three integers — the number of moves in the game and the required values $$$a$$$ and $$$b$$$.


Sample Inputs:
7
11
3 1 4 1 5 9 2 6 5 3 5
1
1000
3
1 1 1
13
1 2 3 4 5 6 7 8 9 10 11 12 13
2
2 1
6
1 1 1 1 1 1
7
1 1 1 1 1 1 1

Sample Outputs:
6 23 21
1 1000 0
2 1 2
6 45 46
2 2 1
3 4 2
4 4 3

Solution (Python):
import math
import sys
from os import path
YES = 'YES'
NO = 'NO'
N = 100000.0 + 7
if path.exists('in.txt'):
    sys.stdin = open('in.txt', 'r')
    sys.stdout = open('out.txt', 'w')

def solve():
    n = int(input())
    a = list(map(int, input().split(' ')))
    turns = 0
    (i, j) = (0, n - 1)
    (ilast, jlast) = (0, 0)
    (iscore, jscore) = (0, 0)
    while i <= j:
        if turns % 2 == 0:
            isum = 0
            while i <= j and isum <= jlast:
                isum += a[i]
                i += 1
            iscore += isum
            ilast = isum
        else:
            jsum = 0
            while i <= j and jsum <= ilast:
                jsum += a[j]
                j -= 1
            jscore += jsum
            jlast = jsum
        turns += 1
    print(turns, iscore, jscore)
tt = int(input())
for _ in range(tt):
    solve()

Solution (Python):
import math
import sys
from os import path
YES = 'YES'
NO = 'NO'
N = 100000.0 + 7
if path.exists('in.txt'):
    sys.stdin = open('in.txt', 'r')
    sys.stdout = open('out.txt', 'w')

def solve():
    n = int(input())
    a = list(map(int, input().split(' ')))
    turns = 0
    (i, j) = (0, n - 1)
    (ilast, jlast) = (0, 0)
    (iscore, jscore) = (0, 0)
    while i <= j:
        if turns % 2 == 0:
            isum = 0
            while i <= j and isum <= jlast:
                isum += a[i]
                i += 1
            iscore += isum
            ilast = isum
        else:
            jsum = 0
            while i <= j and jsum <= ilast:
                jsum += a[j]
                j -= 1
            jscore += jsum
            jlast = jsum
        turns += 1
    print(turns, iscore, jscore)
tt = int(input())
for _ in range(tt):
    solve()

Solution (Python):
def main():
    T = int(input())
    for _ in range(T):
        n = int(input())
        arr = [int(i) for i in input().split()]
        answer = getSizes(arr, n)
        for no in answer:
            print(no, end=' ')
        print()

def getSizes(arr, n):
    alice_index = 1
    bob_index = n - 1
    size = arr[0]
    bob_eaten = 0
    alice_eaten = arr[0]
    moves = 1
    bob_turn = True
    alice_turn = False
    while alice_index <= bob_index:
        if bob_turn:
            bob_current = 0
            while True:
                bob_eaten += arr[bob_index]
                bob_current += arr[bob_index]
                if bob_index == alice_index:
                    moves += 1
                    return [moves, alice_eaten, bob_eaten]
                bob_index -= 1
                if bob_current > size:
                    size = bob_current
                    moves += 1
                    alice_turn = True
                    bob_turn = False
                    break
        elif alice_turn:
            alice_current = 0
            while True:
                alice_eaten += arr[alice_index]
                alice_current += arr[alice_index]
                if alice_index == bob_index:
                    moves += 1
                    return [moves, alice_eaten, bob_eaten]
                alice_index += 1
                if alice_current > size:
                    size = alice_current
                    moves += 1
                    alice_turn = False
                    bob_turn = True
                    break
    return [moves, alice_eaten, bob_eaten]
main()