POJ 3253 Fence Repairを解く

3253 -- Fence Repair
まぁなんか適当にソートしてゴリゴリっとやればええんじゃろ?
オラ!
→TLE
効率悪かったか?
もう少し良くしてみるか
→WA
WAナンデ!?!?!?
でかい値適当に打ち込んでみるか!
→(出力)-37213421
負の値...あっ(察し)オーバーフローかよなにやっとんねん

適当にlong型にしてみる

→AC

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		int N = stdIn.nextInt();
		ArrayList<Long> L = new ArrayList<Long>();
		for(int i = 0; i < N; i++) {
			L.add(stdIn.nextLong());
		}
		long cost = 0;
		Collections.sort(L);
		
		while(L.size() != 1) {
			Long c0 = L.get(0);
			L.remove(0);
			Long c1 = L.get(0);
			L.remove(0);
			int insP = Collections.binarySearch(L,c0+c1);
			L.add((insP < 0)?(insP+1)*-1:insP,c0+c1);
			cost += c0+c1;
		}
		
		System.out.println(cost);
		
	}
}