CSES - Aalto Competitive Programming 2024 - wk3 - Homework - Results
Submission details
Task:Card game
Sender:ZDHKLV
Submission time:2024-09-12 12:42:31 +0300
Language:Python3 (CPython3)
Status:READY
Result:
Test results
testverdicttime
#1--details
#2--details
#3--details
#4--details
#5--details
#6--details
#7--details
#8--details
#9--details
#10--details
#11--details
#12--details
#13--details
#14--details
#15--details

Code

def solve(left, right, x, r):

    if (left, right) in r:
        return r[(left, right)]
    
    if right - left < 3:
        return 0
    elif right - left == 3:
        return x[left+1]
    else:
        output = 0
        
        for i in range(left, right-2):
            a, b = left, i
            val = x[i+1]
            c, d = i+3, right
            output = max(output, solve(a, b, x, r) + val + solve(c, d, x, r))

        r[(left, right)] = output
        return output

def solve2(left, right, x, r):

    if (left, right) in r:
        return r[(left, right)]
    
    if right - left < 3:
        return 0
    elif right - left == 3:
        return x[left+1]
    elif right - left == 4:
        return max(x[left+1], x[left+2])
    else:
        output = max(
            x[left+1] + solve2(left+3, right, x, r),
            x[left+2] + solve2(left+4, right, x, r),
            x[left+3] + solve2(left+5, right, x, r)
        )
        r[(left, right)] = output
        return output

def iterative(x):

    n = len(x)
    r = [0] * (n+1)
    r[3] = x[-2]
    r[4] = max(x[-3], x[-2])

    for i in range(5, n+1):
        r[i] = max(x[n-i+1] + r[i-3], x[n-i+2] + r[i-4], x[n-i+3] + r[i-5])

    return r[n]

# int(input(""))
# print(iterative([int(e) for e in input("").split(" ")]))

from random import randint

N = 5*10**2
X = [ randint(1, 10**3) for _ in range(N) ]

print(solve(0, N, X, {}))
print(solve2(0, N, X, {}))
print(iterative(X))

Test details

Test 1

Verdict:

input
5
9 4 1 6 6

correct output
6

user output
(empty)

Test 2

Verdict:

input
6
5 6 2 4 10 1

correct output
16

user output
(empty)

Test 3

Verdict:

input
10
8 9 10 2 7 1 10 10 1 4

correct output
26

user output
(empty)

Test 4

Verdict:

input
100
1 8 8 5 7 10 9 4 8 10 6 3 8 7 ...

correct output
243

user output
(empty)

Test 5

Verdict:

input
1000
10 7 5 6 5 2 5 3 2 2 1 6 8 7 8...

correct output
2230

user output
(empty)

Test 6

Verdict:

input
10000
9 1 8 2 6 5 1 3 3 10 6 3 9 3 1...

correct output
22363

user output
(empty)

Test 7

Verdict:

input
100000
5 5 4 6 8 7 9 6 3 2 5 8 7 3 5 ...

correct output
226636

user output
(empty)

Test 8

Verdict:

input
1000000
5 8 5 7 9 1 9 10 3 6 1 8 3 9 7...

correct output
2259395

user output
(empty)

Test 9

Verdict:

input
1000000
4 5 3 5 4 3 6 7 10 6 3 9 7 9 1...

correct output
2260761

user output
(empty)

Test 10

Verdict:

input
1000000
10 3 6 7 7 10 4 4 5 2 9 4 6 10...

correct output
2260407

user output
(empty)

Test 11

Verdict:

input
3
87 3 123

correct output
3

user output
(empty)

Test 12

Verdict:

input
2
175 95

correct output
0

user output
(empty)

Test 13

Verdict:

input
1
42

correct output
0

user output
(empty)

Test 14

Verdict:

input
1000000
1000 1000 1000 1000 1000 1000 ...

correct output
333333000

user output
(empty)

Test 15

Verdict:

input
1000000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
333333

user output
(empty)