Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)
import numpy as np

def dfs(v, u, k):
    for i in d[v]:
        if i[0] == u:
            continue
        dnp[i[0]] = k + i[1]
        dfs(i[0], v, k + i[1])

N = int(input())
d = [[] for _ in range(N)]
for i in range(N - 1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    d[a].append((b, c))
    d[b].append((a, c))
kkyori = [0] * N
dnp = np.array(kkyori)
Q, K = map(int, input().split())
dfs(K - 1, -1, 0)
for i in range(Q):
    x, y = map(int, input().split())
    print(dnp[x - 1] + dnp[y - 1])

Fixed submission:
import sys
sys.setrecursionlimit(10**6)

def dfs(v, u, k):
    for i in d[v]:
        if i[0] == u:
            continue
        kkyori[i[0]] = k + i[1]
        dfs(i[0], v, k + i[1])

N = int(input())
d = [[] for _ in range(N)]
for i in range(N - 1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    d[a].append((b, c))
    d[b].append((a, c))
kkyori = [0] * N

Q, K = map(int, input().split())
dfs(K - 1, -1, 0)
for i in range(Q):
    x, y = map(int, input().split())
    print(kkyori[x - 1] + kkyori[y - 1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)

def dfs(v, u, k):
    for i in d[v]:
        if i[0] == u:
            continue
        kkyori[i[0]] = k + i[1]
        dfs(i[0], v, k + i[1])

N = int(input())
d = [[] for _ in range(N)]
for i in range(N - 1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    d[a].append((b, c))
    d[b].append((a, c))
kkyori = [0] * N

Q, K = map(int, input().split())
dfs(K - 1, -1, 0)
for i in range(Q):
    x, y = map(int, input().split())
    print(kkyori[x - 1] + kkyori[y - 1])

Fixed submission:
import sys
sys.setrecursionlimit(10**6)

def dfs(v, u, k):
    for i in d[v]:
        if i[0] == u:
            continue
        kkyori[i[0]] = k + i[1]
        dfs(i[0], v, k + i[1])

N = int(input())
d = [[] for _ in range(N)]
for i in range(N - 1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    d[a].append((b, c))
    d[b].append((a, c))
kkyori = [0] * N

Q, K = map(int, input().split())
dfs(K - 1, -1, 0)
for i in range(Q):
    x, y = map(int, input().split())
    print(kkyori[x - 1] + kkyori[y - 1])
Buggy python submission (verdict: Runtime Error):
N = int(input())
a = list(map(int, input().split()))
cnt = 0
cnt2 = 0
cnt3 = 0
for i in a:
    if i % 2 != 0:
        cnt3 += 1
    elif i % 4 == 0:
        cnt += 1
    else:
    cnt2 += 1
if cnt2 == 0:
    if cnt3 <= cnt + 1:
        print('Yes')
    else:
        print('No')
else:
    if cnt3 <= cnt:
        print('Yes')
    else:
        print('No')

Fixed submission:
N = int(input())
a = list(map(int, input().split()))
cnt = 0
cnt2 = 0
cnt3 = 0
for i in a:
    if i % 2 != 0:
        cnt3 += 1
    elif i % 4 == 0:
        cnt += 1
    else:
        cnt2 += 1

if cnt2 == 0:
    if cnt3 <= cnt + 1:
        print('Yes')
    else:
        print('No')
else:
    if cnt3 <= cnt:
        print('Yes')
    else:
        print('No')
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = list(map(int, input().split()))
ans = 10 ** 10
for i in range(1, N):
    ans = min(ans, abs(sum(a[:i]) - sum(a[i:])))

print(ans)

Fixed submission:
N = int(input())
a = list(map(int, input().split()))
ans = 10 ** 10
total = sum(a)
tmp = 0
for i in range(N - 1):
    tmp += a[i]
    ans = min(ans, abs(total - (2 * tmp)))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = list(map(int, input().split()))
ans = 10 ** 10
for i in range(1, N):
    ans = min(ans, abs(sum(a[:i]) - sum(a[i:])))

print(ans)

Fixed submission:
N = int(input())
a = list(map(int, input().split()))
ans = 10 ** 10
total = sum(a)
tmp = 0
for i in range(N - 1):
    tmp += a[i]
    ans = min(ans, abs(total - (2 * tmp)))

print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
tLst = [[] for _ in range(M)]
vLst = [0] * N

for i in range(M):
    a, b = map(int, input().split())
    tLst[a - 1].append(b - 1)
    tLst[b - 1].append(a - 1)

def dfs(n, index, v, ans):
    vLst[index] = 1
    if n == N - 1:
        ans += 1
        return ans
    for i in tLst[index]:
        if v == i:
            continue
        elif vLst[i] == 1:
            continue
        ans = dfs(n + 1, i, index, ans)
        vLst[i] = 0
    return ans
print(dfs(0, 0, 0, 0))

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
tLst = [[] for _ in range(N)]
vLst = [0] * N

for i in range(M):
    a, b = map(int, input().split())
    tLst[a - 1].append(b - 1)
    tLst[b - 1].append(a - 1)

def dfs(n, index, v, ans):
    vLst[index] = 1
    if n == N - 1:
        ans += 1
        return ans
    for i in tLst[index]:
        if v == i:
            continue
        elif vLst[i] == 1:
            continue
        ans = dfs(n + 1, i, index, ans)
        vLst[i] = 0
    return ans
print(dfs(0, 0, 0, 0))
Buggy python submission (verdict: Runtime Error):
from scipy.sparse.csgraph import dijkstra

INF = 10 ** 18
N, M = map(int, input().split())
d = [[INF for _ in range(N)] for _ in range(N)]
abc = []

for i in range(M):
    a, b, c = map(int, input().split())
    abc.append([a - 1, b - 1, c])
    d[a - 1][b - 1] = c
    d[b - 1][a - 1] = c

dtra = dijkstra(d)
minlst = [INF] * N
for i in range(N):
    for j in range(N):
        if dtra[i][j] != 0:
            minlst[i] = min(minlst[i], dtra[i][j])

cnt = 0
for i in range(M):
    if int(minlst[abc[i][1]]) < abc[i][2]:
        cnt += 1

print(cnt)

Fixed submission:
from scipy.sparse.csgraph import floyd_warshall

INF = 10 ** 18
N, M = map(int, input().split())
d = [[INF for _ in range(N)] for _ in range(N)]
abc = []

for i in range(M):
    a, b, c = map(int, input().split())
    abc.append([a - 1, b - 1, c])
    d[a - 1][b - 1] = c
    d[b - 1][a - 1] = c

wf = floyd_warshall(d)
cnt = 0
for i in range(M):
    if abc[i][2] > wf[abc[i][0]][abc[i][1]]:
        cnt += 1


print(cnt)

Buggy python submission (verdict: Runtime Error):
from scipy.sparse.csgraph import dijkstra

INF = 10 ** 18
N, M = map(int, input().split())
d = [[INF for _ in range(N)] for _ in range(N)]
abc = []

for i in range(M):
    a, b, c = map(int, input().split())
    abc.append([a - 1, b - 1, c])
    d[a - 1][b - 1] = c
    d[b - 1][a - 1] = c

dtra = dijkstra(d)
minlst = [INF] * N
for i in range(N):
    for j in range(N):
        if dtra[i][j] != 0:
            minlst[i] = min(minlst[i], dtra[i][j])

cnt = 0
for i in range(M):
    if int(minlst[abc[i][1]]) < abc[i][2]:
        cnt += 1

print(cnt)

Fixed submission:
from scipy.sparse.csgraph import floyd_warshall

INF = 10 ** 18
N, M = map(int, input().split())
d = [[INF for _ in range(N)] for _ in range(N)]
abc = []

for i in range(M):
    a, b, c = map(int, input().split())
    abc.append([a - 1, b - 1, c])
    d[a - 1][b - 1] = c
    d[b - 1][a - 1] = c

wf = floyd_warshall(d)
cnt = 0
for i in range(M):
    if abc[i][2] > wf[abc[i][0]][abc[i][1]]:
        cnt += 1


print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
X = input()

for i in range(len(X)):
    X = X.replace('ST', '')

print(len(X))


Fixed submission:
X = input()
xlst = []
xlst.append(X[0])
for i in range(1, len(X)):
    if X[i] == 'S':
        xlst.append(X[i])
    else:
        if xlst != [] and xlst[-1] == 'T':
            xlst.append(X[i])
        elif xlst != [] and xlst[-1] == 'S':
            xlst.pop()
        else:
            xlst.append(X[i])

print(len(xlst))

Buggy python submission (verdict: Time Limit Exceeded):
X = input()

for i in range(len(X)):
    X = X.replace('ST', '')

print(len(X))

Fixed submission:
X = input()
xlst = []
xlst.append(X[0])
for i in range(1, len(X)):
    if X[i] == 'S':
        xlst.append(X[i])
    else:
        if xlst != [] and xlst[-1] == 'T':
            xlst.append(X[i])
        elif xlst != [] and xlst[-1] == 'S':
            xlst.pop()
        else:
            xlst.append(X[i])

print(len(xlst))
Buggy python submission (verdict: Time Limit Exceeded):
import string
abc = string.ascii_lowercase
s = list(input())
K = int(input())
index = 0
while K > 0:
    if index == len(s) - 1:
        s[index] = abc[(abc.index(s[index]) + K) % 26]
        break
    tmp = 26 - abc.index(s[index])
    if tmp == 26: continue
    if K >= tmp:
        s[index] = 'a'
        index += 1
        K -= tmp
    else:
        index += 1

for i in s:
    print(i, end='')

Fixed submission:
import string
abc = string.ascii_lowercase
s = list(input())
K = int(input())
index = 0
while K > 0:
    if index == len(s) - 1:
        s[index] = abc[(abc.index(s[index]) + K) % 26]
        break
    tmp = 26 - abc.index(s[index])
    if tmp == 26:
        index += 1
        continue
    if K >= tmp:
        s[index] = 'a'
        index += 1
        K -= tmp
    else:
        index += 1

for i in s:
    print(i, end='')

Buggy python submission (verdict: Time Limit Exceeded):
import string
abc = string.ascii_lowercase
s = list(input())
K = int(input())
index = 0
while K > 0:
    if index == len(s) - 1:
        s[index] = abc[(abc.index(s[index]) + K) % 26]
        break
    tmp = 26 - abc.index(s[index])
    if tmp == 26: continue
    if K >= tmp:
        s[index] = 'a'
        index += 1
        K -= tmp
    else:
        index += 1

for i in s:
    print(i, end='')

Fixed submission:
import string
abc = string.ascii_lowercase
s = list(input())
K = int(input())
index = 0
while K > 0:
    if index == len(s) - 1:
        s[index] = abc[(abc.index(s[index]) + K) % 26]
        break
    tmp = 26 - abc.index(s[index])
    if tmp == 26:
        index += 1
        continue
    if K >= tmp:
        s[index] = 'a'
        index += 1
        K -= tmp
    else:
        index += 1

for i in s:
    print(i, end='')
Buggy python submission (verdict: Runtime Error):
S = input()
p = [1] * (len(S) - 1)
sumlst = [0] * len(S)
ans = 0
def dfs(i):
    global ans
    if i == (len(S) - 1):
        tmp = ''
        if p[0] == 1:
            tmp += S[0] + '+' + S[1]
        else:
            tmp += S[0] + S[1]
        for i in range(1, len(p)):
            if p[i] == 1:
                tmp += '+' + S[i + 1]
            else:
                tmp += S[i + 1]
        ans += eval(tmp)
        return

    p[i] = 1
    dfs(i + 1)
    p[i] = 0
    dfs(i + 1)
    return

dfs(0)

print(ans)

Fixed submission:
S = input()
p = [1] * (len(S) - 1)
sumlst = [0] * len(S)
ans = 0
def dfs(i):
    global ans
    if i == (len(S) - 1):
        tmp = ''
        if p[0] == 1:
            tmp += S[0] + '+' + S[1]
        else:
            tmp += S[0] + S[1]
        for i in range(1, len(p)):
            if p[i] == 1:
                tmp += '+' + S[i + 1]
            else:
                tmp += S[i + 1]
        ans += eval(tmp)
        return

    p[i] = 1
    dfs(i + 1)
    p[i] = 0
    dfs(i + 1)
    return
if len(S) == 1:
    print(int(S))
    exit()
dfs(0)

print(ans)
Buggy python submission (verdict: Runtime Error):
N, A = map(int, input().split())
x = list(map(int, input().split()))
maxX = max(x)
dp = [[[0 for _ in range(N * maxX + 1)] for _ in range(N + 1)] for _ in range(N + 1)]
dp[0][0][0] = 1
for i in range(N):
    for j in range(N + 1):
        for k in range(N * A + 1):
            if dp[i][j][k] == 0: continue
            dp[i + 1][j][k] += dp[i][j][k]
            dp[i + 1][j + 1][k + x[i]] += dp[i][j][k]

ans = 0
for i in range(1, N + 1):
    ans += dp[N][i][A * i]
print(ans)

Fixed submission:
N, A = map(int, input().split())
x = list(map(int, input().split()))
maxX = max(max(x), A)
dp = [[[0 for _ in range(N * maxX + 1)] for _ in range(N + 1)] for _ in range(N + 1)]
dp[0][0][0] = 1
for i in range(N):
    for j in range(N + 1):
        for k in range(N * A + 1):
            if dp[i][j][k] == 0: continue
            dp[i + 1][j][k] += dp[i][j][k]
            dp[i + 1][j + 1][k + x[i]] += dp[i][j][k]

ans = 0
for i in range(1, N + 1):
    ans += dp[N][i][A * i]
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline

    N = int(input())
    P = []
    for _ in range(N):
        x, y = map(int, input().split())
        P.append((x, y))

    def calc_dist(pi):
        ret = -1
        reti = -1
        px, py = P[pi]
        for i, (x, y) in enumerate(P):
            d = abs(px - x) + abs(py - y)
            if d > ret:
                ret = d
                reti = i
        return ret, reti

    prev = -2
    ans = -1

    used = {0}
    from random import randrange

    i = 0
    while True:
        ret, i = calc_dist(i)
        if ret == prev:
            ans = ret
        if ~ans and i in used:
            break
        if prev < ret:
            prev = ret

        if len(used) == N:
            break

    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys
    input = sys.stdin.readline

    N = int(input())
    P = []
    M = []
    for _ in range(N):
        x, y = map(int, input().split())
        P.append(x + y)
        M.append(x - y)
    P.sort()
    M.sort()
    ans = max(P[-1] - P[0], M[-1] - M[0])
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline

    N = int(input())
    P = []
    for _ in range(N):
        x, y = map(int, input().split())
        P.append((x, y))

    def calc_dist(pi):
        ret = -1
        reti = -1
        px, py = P[pi]
        for i, (x, y) in enumerate(P):
            d = abs(px - x) + abs(py - y)
            if d > ret:
                ret = d
                reti = i
        return ret, reti

    prev = -2
    ans = -1

    used = {0}
    from random import randrange

    i = 0
    while True:
        ret, i = calc_dist(i)
        if ret == prev:
            ans = ret
            if i in used:
                break
        if prev < ret:
            prev = ret

        if len(used) == N:
            break

        while i in used:
            i = randrange(0, N)

    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys
    input = sys.stdin.readline

    N = int(input())
    P = []
    M = []
    for _ in range(N):
        x, y = map(int, input().split())
        P.append(x + y)
        M.append(x - y)
    P.sort()
    M.sort()
    ans = max(P[-1] - P[0], M[-1] - M[0])
    print(ans)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
# 解説放送

def main():
    N, K = map(int, input().split())
    *P, = (int(x) - 1 for x in input().split())
    *C, = map(int, input().split())

    ans = -10**9 - 1

    for si in range(N):
        g = []
        tot = 0
        cur = si
        while True:
            cur = P[cur]
            g.append(C[cur])
            tot += C[cur]
            if cur == si or len(g) == K:
                break

        L = len(g)
        t = 0
        for i, x in enumerate(g, start=1):
            t += x
            now = t
            if tot > 0:
                rep = (K - i) // L
                now += tot * rep

            ans = max(ans, now)

    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
# 解説放送

def main():
    N, K = map(int, input().split())
    *P, = (int(x) - 1 for x in input().split())
    *C, = map(int, input().split())

    ans = -10**9 - 1

    for si in range(N):
        g = []
        tot = 0
        cur = si
        while True:
            cur = P[cur]
            g.append(C[cur])
            tot += C[cur]
            if cur == si or len(g) == K:
                break

        L = len(g)
        t = 0
        for i, x in enumerate(g, start=1):
            t += x
            now = t
            if tot > 0:
                rep = (K - i) // L
                now += tot * rep

            ans = max(ans, now)

    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
def main():
    N, K = map(int, input().split())
    *P, = (int(x) - 1 for x in input().split())
    *C, = map(int, input().split())

    score = []
    checked = [0] * N
    for v in range(N):
        if checked[v]:
            continue
        t = []
        for _ in range(N):
            if checked[v]:
                break
            checked[v] = 1
            t.append(C[v])
            v = P[v]
        score.append(t)

    print(f'{score=}')

    ans = 0
    for loop_lst in score:
        L = len(loop_lst)
        s = sum(loop_lst)
        t = 0
        if s > 0:
            q, K = divmod(K, L)
            t += q * s
        K = min(K, L - 1)
        acc = [0]
        for x in loop_lst:
            acc.append(acc[-1] + x)
        add_v = 0
        for j in range(N)
        for i in range(K):
            add_v = max(acc[i+j] - acc[j])
        ans = max(ans, t + add_v)
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
# 解説放送

def main():
    N, K = map(int, input().split())
    *P, = (int(x) - 1 for x in input().split())
    *C, = map(int, input().split())

    ans = -10**9 - 1

    for si in range(N):
        g = []
        tot = 0
        cur = si
        while True:
            cur = P[cur]
            g.append(C[cur])
            tot += C[cur]
            if cur == si or len(g) == K:
                break

        L = len(g)
        t = 0
        for i, x in enumerate(g, start=1):
            t += x
            now = t
            if tot > 0:
                rep = (K - i) // L
                now += tot * rep

            ans = max(ans, now)

    print(ans)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    from math import ceil

    N, K = map(int, input().split())
    *A, = map(int, input().split())

    def binary_search(*, ok, ng, func):
        for _ in range(80):
            mid = (ok + ng) / 2
            if func(mid):
                ok = mid
            else:
                ng = mid
        return ok

    def is_ok(mid):
        cnt = 0
        for x in A:
            cnt += x // mid
            if x % mid == 0:
                cnt -= 1
        return cnt <= K

    ma = max(A)
    res = binary_search(ok=ma, ng=0, func=is_ok)

    print(ceil(res))


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    from math import ceil

    N, K = map(int, input().split())
    *A, = map(int, input().split())

    def binary_search(*, ok, ng, func):
        for _ in range(60):
            mid = (ok + ng) / 2
            if func(mid):
                ok = mid
            else:
                ng = mid
        return ok

    def is_ok(mid):
        cnt = 0
        for x in A:
            q, r = divmod(x, mid)
            cnt += q
            if r == 0:
                cnt -= 1
        return cnt <= K

    ma = max(A)
    res = binary_search(ok=ma, ng=0, func=is_ok)

    print(ceil(res))


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    MOD = 998244353

    A, B, H, W = map(int, input().split())

    dp = [[0] * (W + 1) for _ in range(H + 1)]
    dp[A][B] = 1

    for h in range(A, H + 1):
        for w in range(B, W + 1):
            if h == A and w == B: continue
            t = 0
            if h > A:
                t += dp[h - 1][w] * w
            if w > B:
                t += dp[h][w - 1] * h
            if h > A and w > B:
                t -= dp[h - 1][w - 1] * (h - 1) * (w - 1)
            dp[h][w] = t % MOD

    print(dp[H][W])


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    MOD = 998244353

    A, B, H, W = map(int, input().split())

    dp = [[0] * (W + 1) for _ in range(H + 1)]
    dp[A][B] = 1

    for h in range(A, H + 1):
        for w in range(B, W + 1):
            if h == A and w == B: continue
            t = 0
            if h > A:
                t += dp[h - 1][w] * w
            if w > B:
                t += dp[h][w - 1] * h
            if h > A and w > B:
                t -= dp[h - 1][w - 1] * (h - 1) * (w - 1)
            dp[h][w] = t % MOD

    print(dp[H][W])


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, K = map(int, input().split())
    *A, = map(int, input().split())

    for _ in range(K):
        B = [0] * (N + 1)
        for i, x in enumerate(A):
            B[max(0, i - x)] += 1
            B[min(N - 1, i + x) + 1] -= 1
        for i in range(N):
            B[i + 1] += B[i]
        B.pop()
        if A == B: break
        A = B
    print(*A)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    N, K = map(int, input().split())
    *A, = map(int, input().split())

    for _ in range(min(K, 50)):
        B = [0] * (N + 1)
        for j in range(N):
            d = A[j]
            B[max(j - d, 0)] += 1
            B[min(j + d, N - 1) + 1] -= 1
        for i in range(N):
            B[i + 1] += B[i]
        A = B
    print(*A[:-1])


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
#
# sys.setrecursionlimit(10 ** 7)
#
# (int(x)-1 for x in input().split())
# rstrip()
#
# def binary_search(*, ok, ng, func):
#     while abs(ok - ng) > 1:
#         mid = (ok + ng) // 2
#         if func(mid):
#             ok = mid
#         else:
#             ng = mid
#     return ok
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    *s, = map(int, input())  # <= 2*10**5

    r = [0] * 2019
    r[0] = 1

    ans = 0

    t = 0
    p = 1
    for x in reversed(s):
        t = (t + x * p) % 2019
        p *= 10
        ans += r[t]
        r[t] += 1

    print(ans)


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
# 
# sys.setrecursionlimit(10 ** 7)
# 
# (int(x)-1 for x in input().split())
# rstrip()
#
# def binary_search(*, ok, ng, func):
#     while abs(ok - ng) > 1:
#         mid = (ok + ng) // 2
#         if func(mid):
#             ok = mid
#         else:
#             ng = mid
#     return ok


Fixed submission:
def main():
    *s, = map(int, input())  # <= 2*10**5

    r = [0] * 2019
    r[0] = 1

    ans = 0

    t = 0
    p = 1
    for x in reversed(s):
        t = (t + x * p) % 2019
        p = (p * 10) % 2019
        ans += r[t]
        r[t] += 1

    print(ans)


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
# 
# sys.setrecursionlimit(10 ** 7)
# 
# (int(x)-1 for x in input().split())
# rstrip()
#
# def binary_search(*, ok, ng, func):
#     while abs(ok - ng) > 1:
#         mid = (ok + ng) // 2
#         if func(mid):
#             ok = mid
#         else:
#             ng = mid
#     return ok

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    *s, = map(int, input())  # <= 2*10**5

    r = [0] * 2019
    r[0] = 1

    ans = 0

    t = 0
    p = 1
    for x in reversed(s):
        t = (t + x * p) % 2019
        p *= 10
        ans += r[t]
        r[t] += 1

    print(ans)


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
# 
# sys.setrecursionlimit(10 ** 7)
# 
# (int(x)-1 for x in input().split())
# rstrip()
#
# def binary_search(*, ok, ng, func):
#     while abs(ok - ng) > 1:
#         mid = (ok + ng) // 2
#         if func(mid):
#             ok = mid
#         else:
#             ng = mid
#     return ok


Fixed submission:
def main():
    *s, = map(int, input())  # <= 2*10**5

    r = [0] * 2019
    r[0] = 1

    ans = 0

    t = 0
    p = 1
    for x in reversed(s):
        t = (t + x * p) % 2019
        p = (p * 10) % 2019
        ans += r[t]
        r[t] += 1

    print(ans)


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
# 
# sys.setrecursionlimit(10 ** 7)
# 
# (int(x)-1 for x in input().split())
# rstrip()
#
# def binary_search(*, ok, ng, func):
#     while abs(ok - ng) > 1:
#         mid = (ok + ng) // 2
#         if func(mid):
#             ok = mid
#         else:
#             ng = mid
#     return ok