from __future__ import annotations import sys from dataclasses import dataclass @dataclass class E: v: int p: E n: E V0 = None p = None L = [] for l in sys.stdin.read().splitlines(): e = E(int(l),p,None) L.append(e) if l == "0": V0 = e if p is not None: p.n = e p = e L[-1].n = L[0] L[0].p = L[-1] def swap(l,m): k = l.p n = m.n k.n = m n.p = l l.n = n m.p = k l.p = m m.n = l for l in L: if l.v > 0: for _ in range(l.v): swap(l,l.n) elif l.v < 0: for _ in range(-l.v): swap(l.p,l) S = 0 s = V0 for i in range(3000+1): if i in [1000,2000,3000]: S += s.v s = s.n print(S)