function Main(input) { const lines = input.split('\n').map(x => x.trim()).filter(Boolean); const [n, x] = lines[0].split(' ').map(Number); const a = lines[1].split(' ').map(Number); let head = 0; let tail = 0; let length = 0; let best = 0; let current = 0; let tailMoved = false; while (true) { if (head === tail) { current += a[head]; head = (head + 1) % a.length; length++; } else { if (current > x) { current -= a[tail]; tail = (tail + 1) % a.length; tailMoved = true; length--; } else { current += a[head]; head = (head + 1) % a.length; length++; } } if (tailMoved && tail === 0 || length > a.length) { break; } if (current <= x) { best = Math.max(best, current); } } console.log(best); } Main(require("fs").readFileSync("/dev/stdin", "utf8"));