結果

問題 No.2694 The Early Bird Catches The Worm
ユーザー MM
提出日時 2024-02-15 23:22:08
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,150 bytes
コンパイル時間 1,871 ms
コンパイル使用メモリ 169,932 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-09-28 22:35:27
合計ジャッジ時間 10,009 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 13 WA * 59
権限があれば一括ダウンロードができます

ソースコード

diff #

// WA: overflow 
#include<bits/stdc++.h>
#define chmin(x,y) (x) = min((x),(y))
#define chmax(x,y) (x) = max((x),(y))
using namespace std;
// using ll = long long;

int main(){
  // input
  int N, H;
  cin >> N >> H;
  vector<int> A(N),B(N),S(N+1); // S[i]: Bの累積和
  for(int i = 0; i < N; i++)
    cin >> A[i];
  for(int i = 0; i < N; i++){
    cin >> B[i];
    S[i+1] = S[i] + B[i];
  }
  
  // solve: 尺取法
  int ans = 0;
  int tmp = 0, need_hp = 0;
  int r = 0;
  for(int l = 0; l < N; l++){
    // right < n && (right を 1 個進めたときに条件を満たす)
    while(r < N && need_hp + B[r] * (r-l+1) <= H){
      tmp += A[r];
      need_hp += B[r] * (r-l+1);
      r++;
    }
    
    // break した状態で right は条件を満たす最大なので、何かする
    // cout << l << " " << r << " " << need_hp << " " << tmp << endl;
    chmax(ans,tmp);
    
    // left をインクリメントする準備
    if(l == r) r++;
    else{
      tmp -= A[l];
      need_hp -= S[r]-S[l]; // 累積和で差分をO(1)更新できることに気付くのが本問のポイント
    }
  }
  
  // output
  cout << ans << endl;
}
0