結果

問題 No.2601 Very Poor
ユーザー Maksym Shcherban
提出日時 2024-01-12 23:07:37
言語 JavaScript
(node v23.5.0)
結果
AC  
実行時間 256 ms / 2,000 ms
コード長 906 bytes
コンパイル時間 49 ms
コンパイル使用メモリ 6,948 KB
実行使用メモリ 59,840 KB
最終ジャッジ日時 2024-09-30 06:29:50
合計ジャッジ時間 7,095 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

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"));
0