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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
# V7

import math

num_tests = 0
tests_list = []

num_tests  = int(input())
for t in range(num_tests):
    n = int(input())
    line = tuple(map(int, input().split()))
    tests_list.append(dict(
        n = len(line),
        tours = line,
        treshold = sum(line)*3
    ))

def initialize(test_nr):
    livers_list=[]

    global tests_list

    tours_l = []
    tours = tests_list[test_nr]["tours"]
    tests_list[test_nr]["treshold"]
    
    n = len(tours)
    for i in range(len(tours)):

        if i in (0, n-1):

            if tours[i] != 0:
                livers_list.append(2)
            else:
                livers_list.append(0)

            tours_l.append(tours[i])
            
        else:
            if tours[i] != 0:
                livers_list.append(1)
                tours_l.append(tours[i])

    tests_list[test_nr]["tours"] = tuple(tours_l)
    tests_list[test_nr]["n"] = len(livers_list)
    return livers_list

def get_variant(livers_list, cur_pos,test_nr, marker):
    global tests_list

    n = tests_list[test_nr]["n"]
    tours = tests_list[test_nr]["tours"] 
    
    # exit from recursion
    if (cur_pos == n ) or (sum(livers_list) > tests_list[test_nr]["treshold"] ):
        # print("> Final case: ", livers_list, sum(livers_list), marker)
        if sum(livers_list) < tests_list[test_nr]["treshold"] :
            tests_list[test_nr]["treshold"]  = sum(livers_list)
        return

    livers_right =  sum(livers_list[cur_pos+1:])
    livers_left  =  sum(livers_list[:cur_pos])
   
    cur_tours = tours[cur_pos] 
    cur_livers = livers_list[cur_pos]

    # print(">>>", cur_pos, cur_tours, livers_list,  cur_livers, "LR", livers_left, livers_right ,marker)
    
    # to check max possible tours and adjust livers
    while (True):
        if livers_left == 0 and cur_pos > 0:

            new_list=livers_list[:]
            new_list[cur_pos] += 1
            get_variant(new_list, cur_pos+1,test_nr, marker+"a")

            new_list=livers_list[:]
            new_list[cur_pos-1] += 1
            get_variant(new_list, cur_pos+1,test_nr, marker+"b")    
        # endif
        
        potential_tours = 0
        potential_tours += livers_right * livers_left * cur_livers 
        potential_tours += math.ceil((livers_left + livers_right) * (cur_livers * (cur_livers - 1))/2)
        
        if cur_livers >= 3:
            potential_tours += math.ceil((cur_livers * (cur_livers - 1) * (cur_livers - 2))/6)
        # endifa
        
        if potential_tours >= cur_tours: break
        else: 
            # check right zeros and fill it otherwise increase the current position
            flag = True
            for i in range(n-cur_pos):
                if  livers_list[i+cur_pos] == 0:
                    livers_list[i+cur_pos] = 1
                    livers_right += 1
                    flag = False
                    break;               
            
            if flag == True:  cur_livers = cur_livers + 1       
        # endif potential_tours >= cur_tours: break

    livers_list[cur_pos] = cur_livers
    get_variant(livers_list, cur_pos+1,test_nr, marker+"s")
    
# ---------------------!------!-----< test_nr

for t in range(len(tests_list)):
    get_variant(initialize(t), 0, t, "x")    
    print(tests_list[t]["treshold"] )