結果

問題 No.1150 シュークリームゲーム(Easy)
ユーザー ks2m
提出日時 2020-08-07 22:00:31
言語 Java
(openjdk 23)
結果
AC  
実行時間 237 ms / 2,000 ms
コード長 1,182 bytes
コンパイル時間 2,477 ms
コンパイル使用メモリ 79,044 KB
実行使用メモリ 62,476 KB
最終ジャッジ日時 2024-09-24 20:08:26
合計ジャッジ時間 10,431 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.PriorityQueue;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String[] sa = br.readLine().split(" ");
		int s = Integer.parseInt(sa[0]) - 1;
		int t = Integer.parseInt(sa[1]) - 1;
		sa = br.readLine().split(" ");
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(sa[i]);
		}
		br.close();

		long x = 0;
		long y = 0;
		PriorityQueue<Integer> que = new PriorityQueue<>(Collections.reverseOrder());
		for (int i = 0; i < n; i++) {
			int ds = Math.min(Math.min(Math.abs(i - s), Math.abs(n + i - s)),
					Math.abs(n + s - i));
			int dt = Math.min(Math.min(Math.abs(i - t), Math.abs(n + i - t)),
					Math.abs(n + t - i));
			if (ds < dt) x += a[i];
			if (ds > dt) y += a[i];
			if (ds == dt) que.add(a[i]);
		}
		boolean fs = true;
		while (!que.isEmpty()) {
			if (fs) {
				x += que.poll();
			} else {
				y += que.poll();
			}
			fs = !fs;
		}
		System.out.println(x - y);
	}
}
0