結果

問題 No.2601 Very Poor
ユーザー chro_96
提出日時 2024-01-12 22:44:02
言語 C
(gcc 13.3.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 784 bytes
コンパイル時間 819 ms
コンパイル使用メモリ 29,440 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-09-30 06:27:41
合計ジャッジ時間 2,127 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

int main () {
  int n = 0;
  long long x = 0LL;
  long long a[200000] = {};
  
  int res = 0;
  
  long long ans = 0LL;
  long long tmp = 0LL;
  int idx = 0;
  
  res = scanf("%d", &n);
  res = scanf("%lld", &x);
  for (int i = 0; i < n; i++) {
    res = scanf("%lld", a+i);
    ans += a[i];
  }
  
  if (ans <= x) {
    printf("%lld\n", ans);
    return 0;
  }
  
  ans = 0LL;
  for (int i = 0; i < n; i++) {
    if (a[i] > x) {
      tmp = 0LL;
      idx = i+1;
    } else {
      if (idx%n == i) {
        tmp = a[i];
        idx = i+1;
      }
      while (idx%n != i && a[idx%n]+tmp <= x) {
        tmp += a[idx%n];
        idx++;
      }
      if (tmp > ans) {
        ans = tmp;
      }
      tmp -= a[i];
    }
  }
  
  printf("%lld\n", ans);
  return 0;
}
0