Problem: There is an infinitely large pond, which we consider as a number line. In this pond, there are N lotuses floating at coordinates 0, 1, 2, ..., N-2 and N-1. On the lotus at coordinate i, an integer s_i is written.  You are standing on the lotus at coordinate 0. You will play a game that proceeds as follows:    * 1. Choose positive integers A and B. Your score is initially 0.   * 2. Let x be your current coordinate, and y = x+A. The lotus at coordinate x disappears, and you move to coordinate y.     * If y = N-1, the game ends.     * If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.     * If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.   * 3. Let x be your current coordinate, and y = x-B. The lotus at coordinate x disappears, and you move to coordinate y.     * If y = N-1, the game ends.     * If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.     * If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.   * 4. Go back to step 2.  You want to end the game with as high a score as possible. What is the score obtained by the optimal choice of A and B?

Submission attempts (in order):
import sys
import itertools
input = sys.stdin.readline
sys.setrecursionlimit(100000)
# mod = 10 ** 9 + 7
mod = 998244353

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


class V:
    def __init__(self, f):
        self.f = f
        self.v = None
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if self.v is None:
            self.v = n
        else:
            self.v = self.f(self.v, n) 


def f(N, S, A, L, R, exist=False):
    if S == 0:
        return [[0, L - 1]]

    SS = sum(A[L:R + 1])
    if SS < S:
        return []
    if SS == S:
        return [[L, R]]
    if L == R:
        return []

    T1 = f(N, S, A, L + 1, R, exist)
    T2 = f(N, S - A[L], A, L + 1, R, True)

    if not exist and len(T2) > 0:
        for l in T2:
            l[0] = L
    T1.extend(T2)
    return T1


def main():
    N = int(input())
    S = read_list()

    dp = [[0 for _ in range(N)] for __ in range(N)]
    res = V(max)
    res.ud(0)
    for c in range(1, N):
        T = set()
        for k in range(N):
            t1 = (k + 1) * c
            t2 = (N - 1) - t1 
            if t1 >= N - 1:
                break
            
            if t1 == t2:
                break

            if t1 in T or t2 in T: 
                break
        
            dp[c][k + 1] = dp[c][k] + S[t1] + S[t2]
            T.add(t1)
            T.add(t2)
            res.ud(dp[c][k + 1])

    print(res)



if __name__ == "__main__":
    main(

import sys
import itertools
input = sys.stdin.readline
sys.setrecursionlimit(100000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if self.v is None:
            self.v = n
        else:
            self.v = self.f(self.v, n) 


def main():
    N = int(input())
    S = read_list()

    res = V(max, 0)
    for c in range(1, N):
        r = 0
        t1 = 0
        while True:
            t1 += c
            if t1 > N - 1 - c:
                break
            
            if (N - 1) % c == 0 and 2 * t1 >= N - 1:
                break

            r += S[t1] + S[N - 1 - t1]
            res.ud(r)

    print(res)


if __name__ == "__main__":
    main()


Verdicts: Runtime Error

Accepted