Problem:
Score : 400 points

Problem Statement

For an integer N, we will choose a permutation \{P_1, P_2, ..., P_N\} of \{1, 2, ..., N\}.

Then, for each i=1,2,...,N, let M_i be the remainder when i is divided by P_i.

Find the maximum possible value of M_1 + M_2 + \cdots + M_N.

Constraints

N is an integer satisfying 1 \leq N \leq 10^9.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible value of M_1 + M_2 + \cdots + M_N.

Sample Input 1

2

Sample Output 1

1

When the permutation \{P_1, P_2\} = \{2, 1\} is chosen, M_1 + M_2 = 1 + 0 = 1.

Sample Input 2

13

Sample Output 2

78

Sample Input 3

1

Sample Output 3

0

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Runtime Error)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashSet;

public class Main {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String[] inputs = br.readLine().split(" ");
		int n = Integer.parseInt(inputs[0]);

		Deque<Integer>[] A = new Deque[n + 1];
		for ( int r = 1; r <= n; r++ ) {
			A[r] = new ArrayDeque<Integer>();
			inputs = br.readLine().split(" ");
			for ( int c = 0; c < n - 1; c++ ) {
				A[r].push(Integer.parseInt(inputs[c]));
			}
		}

		int days = 0;
		boolean stalemate;
		HashSet<Integer> removed = new HashSet<>(); // その日の試合が成立して除外された選手をここに入れる
		do {
			stalemate = true; // 手詰まりと仮定
			days++;
			removed.clear();
			
			// p:player opp:opponent
			for ( int p = 1; p <= n; p++ ) {
				if(removed.contains(p))continue;
				Integer opp = A[p].peek();
				if(removed.contains(opp))continue;
				if ( opp == null ) continue;
				Integer opps_opp = A[opp].peek();
				if ( opps_opp == null ) continue;

				// 対戦可能相手の対戦可能相手がｐだったら試合成立
				if ( p == opps_opp ) {
					// ループ内でリムーブするとWA（次の日に成立するはずの試合相手が見えてしまう）
					A[p].removeFirst();
					A[opp].removeFirst();
					removed.add(p);
					removed.add(opp);
					stalemate = false;
				}
			}
			// 成立した試合を除外
			for ( Integer i : removed ) A[i].removeFirst();
		// 手詰まりになったら終了
		} while ( !stalemate );

		for ( int i = 1; i <= n; i++ ) {
			if ( A[i].size() != 0 ) {
				System.out.println("-1");
				br.close();
				return;
			}
		}
		System.out.println(days - 1);
		br.close();
	}
}