結果

問題 No.8103 Range Chmax and Maximize Abs Sum
ユーザー Asahi
提出日時 2023-03-31 22:36:04
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,588 bytes
コンパイル時間 2,288 ms
コンパイル使用メモリ 79,096 KB
実行使用メモリ 76,456 KB
最終ジャッジ日時 2024-09-23 01:24:24
合計ジャッジ時間 14,794 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 2 WA * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
import java.math.*;

class Main{

    static void solve(){

        int n = ni();
        long [] A = new long[n];
        long [][] dp = new long[n+1][2];
        for(long [] v : dp) Arrays.fill(v,(long)-1e18);
        for(int i = 0 ; i < n ; i ++ ) {
            A[i] = nl();
        }
        dp[0][0] = A[0];
        dp[0][1] = A[0];
        long ans1 = 0;
        long ans2 = 0;
        for(int i = 0 ; i < n-1 ; i ++ ) {
            // l -> 
            dp[i+1][0] = Math.max(dp[i][0],dp[i+1][0]);
            dp[i+1][1] = Math.max(dp[i+1][1],(long)Math.abs(A[i+1]));
            // --> r
            dp[i+1][0] = Math.max(dp[i+1][0],(long)Math.abs(A[i+1]));
            dp[i+1][1] = Math.max(dp[i][1],dp[i+1][1]);
        }
        for(int i = 0; i < n ; i ++ ) {
            ans1 += dp[i][0];
            ans2 += dp[i][1];
        }
        output.println((long)Math.max(ans1,ans2));
        
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - //

    static int ni(){ return Integer.parseInt(sc.next()); }
    static long nl(){ return Long.parseLong(sc.next()); }
    static double nd(){ return Double.parseDouble(sc.next()); }
    static String ns(){ return sc.next(); }
    static BigInteger bi(){ return sc.nextBigInteger(); }
    static BigDecimal bd(){ return sc.nextBigDecimal(); }

    static PrintWriter output;
    static Scanner sc ;
    public static void main(String[] args){
        output = new PrintWriter(System.out);
        sc = new Scanner(System.in);
        solve();
        output.flush();
    }

}

0