結果

問題 No.297 カードの数式
ユーザー nanophoto12
提出日時 2015-11-23 14:59:00
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,637 bytes
コンパイル時間 2,654 ms
コンパイル使用メモリ 80,664 KB
実行使用メモリ 56,952 KB
最終ジャッジ日時 2024-09-13 17:17:25
合計ジャッジ時間 7,631 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9 WA * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

package com.company;

import java.util.*;

public class Main {

    private static long RemoveLargest(List<Integer> list, int operatorCount)
    {
        long value = 0L;
        int size = list.size();
        for(int i = 0; i < size - operatorCount;i++)
        {
            value *=10L;
            value += list.get(0);
            list.remove(0);
        }
        return value;
    }


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        List<Integer> list = new ArrayList<Integer>(0);
        Integer plusCount = 0;
        Integer minusCount = 0;
        for(int i = 0; i < n;i++)
        {
            String token = scanner.next();
            if(token.equals("+"))
            {
                plusCount++;
                continue;
            }
            if(token.equals("-"))
            {
                minusCount++;
                continue;
            }
            int value = Integer.parseInt(token);
            list.add(value);
        }
        Collections.sort(list);
        Collections.reverse(list);
        Long largest = RemoveLargest(list, plusCount+minusCount);
        Long maxValue = largest;
        for(int i = 0; i < plusCount;i++)
        {
            maxValue += list.get(i);
        }
        for(int i = plusCount;i < plusCount + minusCount;i++)
        {
            maxValue -= list.get(i);
        }
        Long minValue = maxValue;
        if(minusCount > 0)
        {
            minValue = -maxValue;
        }
        System.out.println(maxValue.toString() + " " + minValue.toString());
    }
}
0