結果

問題 No.1007 コイン集め
ユーザー bonKotsubonKotsu
提出日時 2021-06-21 22:54:03
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 45 ms / 1,500 ms
コード長 679 bytes
コンパイル時間 1,660 ms
コンパイル使用メモリ 168,600 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-22 22:54:06
合計ジャッジ時間 2,831 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define P pair<int, int>

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n+2);
  rep(i,n) cin >> a[i+1];
  
  if (a[k] == 0) {
    cout << 0 << endl;
    return 0;
  }

  int l = k-1, r = k+1;
  while (l > 0 && a[l] > 1) {
    l--;
  }
  while (r < n && a[r] > 1) {
    r++;
  }
  ll ans = 0;
  if (a[k] >= 2) {
    for (int i = l; i <= r; i++) ans += a[i];
  } else {
    ll cand1 = 0, cand2 = 0;
    for (int i = l; i <= k; i++) cand1 += a[i];
    for (int i = k; i <= r; i++) cand2 += a[i];
    ans = max(cand1, cand2);
  }
  cout << ans << endl;


}
0