結果

問題 No.1007 コイン集め
ユーザー ntk3264ntk3264
提出日時 2020-04-03 03:49:04
言語 Java21
(openjdk 21)
結果
AC  
実行時間 604 ms / 1,500 ms
コード長 1,269 bytes
コンパイル時間 2,225 ms
コンパイル使用メモリ 74,052 KB
実行使用メモリ 61,268 KB
最終ジャッジ日時 2023-09-11 03:08:45
合計ジャッジ時間 11,655 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
56,044 KB
testcase_01 AC 127 ms
55,820 KB
testcase_02 AC 127 ms
55,640 KB
testcase_03 AC 128 ms
55,540 KB
testcase_04 AC 128 ms
55,780 KB
testcase_05 AC 126 ms
55,492 KB
testcase_06 AC 126 ms
55,472 KB
testcase_07 AC 125 ms
55,748 KB
testcase_08 AC 126 ms
55,844 KB
testcase_09 AC 127 ms
55,808 KB
testcase_10 AC 126 ms
56,224 KB
testcase_11 AC 126 ms
55,952 KB
testcase_12 AC 597 ms
60,464 KB
testcase_13 AC 604 ms
60,896 KB
testcase_14 AC 587 ms
60,780 KB
testcase_15 AC 482 ms
60,724 KB
testcase_16 AC 467 ms
60,404 KB
testcase_17 AC 493 ms
60,676 KB
testcase_18 AC 594 ms
61,188 KB
testcase_19 AC 586 ms
60,780 KB
testcase_20 AC 582 ms
60,828 KB
testcase_21 AC 588 ms
61,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter;
import java.util.*;

public class Main {

    Scanner sc = new Scanner(System.in);

    PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) {
        new Main().run();
    }

    void run() {

        int n = sc.nextInt();
        int k = sc.nextInt()-1;

        int[] arr = new int[n];
        for (int i=0; i<n; i++) arr[i] = sc.nextInt();

        if (arr[k]==0) {
            out.print(0);
            out.flush();
            return;
        }

        long left = 0;
        long right = 0;

        long ans = 0;

        L:
        for (int i=k-1; i>=0; i--) {
            if (arr[i]==0) break L;
            if (arr[i]==1) {
                left++;
                break L;
            }
            left += arr[i];
        }

        R:
        for (int i=k+1; i<n; i++) {
            if (arr[i]==0) break R;
            if (arr[i]==1) {
                right++;
                break R;
            }
            right += arr[i];
        }

        if (arr[k]==1) {
            ans = (left<right) ? right+1 : left+1;
            out.print(ans);
            out.flush();
            return;
        }

        ans += left+right+arr[k];
        out.print(ans);
        out.flush();

    }

}
0