import strutils, sequtils, math, algorithm var N, V = stdin.readLine.split.map parseInt cnt = 0 flg: bool proc check = var best = 0 if V.len <= 4: for i in 0 ..< 2 ^ V.len: var pass: bool cnt_s = 0 for j in 0 ..< V.len: if pass: pass = false elif i.toBin(V.len)[j] == '1': cnt_s += V[j] pass = true best = max(best, cnt_s) echo best + cnt quit() proc eat = cnt += V[0] V.delete 0, 1 flg = false while true: check() if V[0] >= V[1]: eat() else: case V[0] + V[2] - (V[1] + V[3]) of 0: if flg: eat() else: V.reverse flg = true of 1 .. int.high: eat() else: V.delete 0 flg = false