結果

問題 No.505 カードの数式2
ユーザー takeya_okinotakeya_okino
提出日時 2017-06-12 11:30:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 127 ms / 2,000 ms
コード長 1,157 bytes
コンパイル時間 1,987 ms
コンパイル使用メモリ 76,912 KB
実行使用メモリ 41,568 KB
最終ジャッジ日時 2024-04-26 17:46:34
合計ジャッジ時間 6,802 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
41,284 KB
testcase_01 AC 120 ms
41,076 KB
testcase_02 AC 114 ms
40,372 KB
testcase_03 AC 125 ms
41,108 KB
testcase_04 AC 113 ms
40,244 KB
testcase_05 AC 127 ms
41,180 KB
testcase_06 AC 124 ms
41,252 KB
testcase_07 AC 105 ms
40,060 KB
testcase_08 AC 121 ms
40,944 KB
testcase_09 AC 121 ms
40,824 KB
testcase_10 AC 119 ms
41,180 KB
testcase_11 AC 107 ms
40,056 KB
testcase_12 AC 111 ms
40,220 KB
testcase_13 AC 120 ms
41,280 KB
testcase_14 AC 123 ms
40,996 KB
testcase_15 AC 124 ms
41,192 KB
testcase_16 AC 124 ms
41,232 KB
testcase_17 AC 114 ms
40,156 KB
testcase_18 AC 112 ms
40,028 KB
testcase_19 AC 111 ms
39,848 KB
testcase_20 AC 117 ms
40,952 KB
testcase_21 AC 119 ms
41,080 KB
testcase_22 AC 120 ms
41,568 KB
testcase_23 AC 113 ms
40,556 KB
testcase_24 AC 119 ms
41,216 KB
testcase_25 AC 122 ms
41,364 KB
testcase_26 AC 108 ms
39,880 KB
testcase_27 AC 105 ms
40,032 KB
testcase_28 AC 105 ms
40,036 KB
testcase_29 AC 122 ms
41,300 KB
testcase_30 AC 111 ms
40,048 KB
testcase_31 AC 127 ms
41,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    long[] a = new long[N];
    for(int i = 0; i < N; i++) {
      a[i] = sc.nextLong();
    }
    // i項までの最大値
    long[] dp1 = new long[N];
    // i項までの最小値
    long[] dp2 = new long[N];
    dp1[0] = a[0];
    dp2[0] = a[0];
    for(int i = 1; i < N; i++) {
      if(a[i] == 0) {
        dp1[i] = Math.max(dp1[i - 1], 0);
        dp2[i] = Math.min(dp2[i - 1], 0);
      } else if(a[i] > 0) {
        long c = Math.max(dp1[i - 1] + a[i], dp1[i - 1] * a[i]);
        c = Math.max(c, dp1[i - 1] / a[i]);
        dp1[i] = c;
        long b = Math.min(dp2[i - 1] - a[i], dp2[i - 1] * a[i]);
        b = Math.min(b, dp2[i - 1] / a[i]);
        dp2[i] = b;
      } else {
        long c = Math.max(dp1[i - 1] - a[i], dp2[i - 1] * a[i]);
        c = Math.max(c, dp2[i - 1] / a[i]);
        dp1[i] = c;
        long b = Math.min(dp2[i - 1] + a[i], dp1[i - 1] * a[i]);
        b = Math.min(b, dp1[i - 1] / a[i]);
        dp2[i] = b;
      }
    }
    System.out.println(dp1[N - 1]);
  }
}
0