summaryrefslogtreecommitdiff
path: root/2010/qual/c/c.py
blob: e13369cfdc2eb9c554f641b2ff658539611faf24 (plain)
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
f = open('2.in', 'r')
o = open('2.out', 'w')

t = int(f.readline().strip())

def c(l):
    while True:
        for x in l:
            yield x

for i in xrange(t):
    (r, k, n) = map(int, f.readline().strip().split(' '))
    g = map(int, f.readline().strip().split(' '))
    
    q = c(g)
    
    profit = 0
    buf = 0
    for j in xrange(r):
        load = 0
        if buf != 0:
            next = buf
            buf = 0
        else:
            next = q.next()
            
        while load + next <= k:
            load += next
            next = q.next()
        buf = next
        profit += load
    
    s = "Case #%d: %s\n" % (i+1, profit)
    o.write(s)