結果

問題 No.297 カードの数式
ユーザー crazybbb3crazybbb3
提出日時 2016-12-05 21:52:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 68 ms / 1,000 ms
コード長 3,504 bytes
コンパイル時間 3,661 ms
コンパイル使用メモリ 86,300 KB
実行使用メモリ 51,340 KB
最終ジャッジ日時 2023-08-26 22:56:22
合計ジャッジ時間 5,315 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
50,976 KB
testcase_01 AC 64 ms
50,840 KB
testcase_02 AC 64 ms
50,988 KB
testcase_03 AC 68 ms
48,896 KB
testcase_04 AC 62 ms
50,976 KB
testcase_05 AC 62 ms
50,900 KB
testcase_06 AC 62 ms
50,928 KB
testcase_07 AC 63 ms
51,260 KB
testcase_08 AC 64 ms
50,924 KB
testcase_09 AC 62 ms
51,020 KB
testcase_10 AC 63 ms
50,984 KB
testcase_11 AC 62 ms
50,904 KB
testcase_12 AC 62 ms
50,952 KB
testcase_13 AC 63 ms
50,876 KB
testcase_14 AC 64 ms
50,832 KB
testcase_15 AC 62 ms
50,828 KB
testcase_16 AC 62 ms
50,800 KB
testcase_17 AC 63 ms
50,788 KB
testcase_18 AC 63 ms
51,340 KB
testcase_19 AC 62 ms
50,824 KB
testcase_20 AC 62 ms
50,872 KB
testcase_21 AC 62 ms
51,116 KB
testcase_22 AC 63 ms
50,816 KB
testcase_23 AC 62 ms
51,132 KB
testcase_24 AC 62 ms
50,852 KB
testcase_25 AC 63 ms
50,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class Main {

    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;

    void solve() throws IOException {
        int n = ni();
        int mcnt = 0;
        int pcnt = 0;
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String s = ns();
            char c = s.charAt(0);
            if (c == '+') pcnt++;
            else if (c == '-') mcnt++;
            else list.add(c - '0');
        }

        Collections.sort(list);
        int idx = list.size() - 1;
        String s = "";
        for (int i = 0; i < n - 2 * (mcnt + pcnt); i++) {
            s += list.get(idx);
            idx--;
        }
        long max = Long.parseLong(s);
        for (int i = 0; i < pcnt; i++) {
            max += list.get(idx);
            idx--;
        }
        for (int i = 0; i < mcnt; i++) {
            max -= list.get(idx);
            idx--;
        }

        out.print(max + " ");

        if (mcnt == 0) {
            idx = 0;
            String[] t = new String[pcnt + 1];
            for (int i = 0; i <= pcnt; i++) {
                t[i] = "";
            }
            for (int i = 0; i < list.size(); i++) {
                t[i % (pcnt + 1)] += list.get(idx);
                idx++;
            }
            long min = 0;
            for (int i = 0; i <= pcnt; i++) {
                min += Long.parseLong(t[i]);
            }

            out.print(min);
        } else {
            idx = list.size() - 1;
            s = "";
            for (int i = 0; i < n - 2 * (mcnt + pcnt); i++) {
                s += list.get(idx);
                idx--;
            }
            long min = list.get(0) - Long.parseLong(s);
            mcnt--;
            for (int i = 0; i < mcnt; i++) {
                min -= list.get(idx);
                idx--;
            }
            for (int i = 0; i < pcnt; i++) {
                min += list.get(idx);
                idx--;
            }

            out.print(min);
        }
    }

    String ns() throws IOException {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine(), " ");
        }
        return tok.nextToken();
    }

    int ni() throws IOException {
        return Integer.parseInt(ns());
    }

    long nl() throws IOException {
        return Long.parseLong(ns());
    }

    double nd() throws IOException {
        return Double.parseDouble(ns());
    }

    String[] nsa(int n) throws IOException {
        String[] res = new String[n];
        for (int i = 0; i < n; i++) {
            res[i] = ns();
        }
        return res;
    }

    int[] nia(int n) throws IOException {
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            res[i] = ni();
        }
        return res;
    }

    long[] nla(int n) throws IOException {
        long[] res = new long[n];
        for (int i = 0; i < n; i++) {
            res[i] = nl();
        }
        return res;
    }

    public static void main(String[] args) throws IOException {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        tok = new StringTokenizer("");
        Main main = new Main();
        main.solve();
        out.close();
    }
}
0