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 C++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include <iostream>
#include <list>

int main(){
  int N;
  std::cin >> N;
  std::list<int> P, P2;
  for(int i=0; i<N; i++){
    P.push_back(i+1);
    P2.push_back(i+1);
  }

  int mod = 0;
  for(int i=N; i>0; i--){
    auto temp_itr = P.begin();
    int temp_mod = 0;
    for(auto itr = P.begin(); itr != P.end(); ++itr) {
      if(i % *itr > temp_mod){
        temp_itr = itr;
        temp_mod = i % *itr;
      }
    }
    P.erase(temp_itr);
    mod += temp_mod;
  }
  int mod2 = 0;
  for(int i=0; i<N; i++){
    auto temp_itr = P2.begin();
    int temp_mod = 0;
    for(auto itr = P2.begin(); itr != P2.end(); ++itr) {
      if(i % *itr > temp_mod){
        temp_itr = itr;
        temp_mod = i % *itr;
      }
    }
    P2.erase(temp_itr);
    mod2 += temp_mod;
  }
  int ret = mod > mod2 ? mod : mod2;
  std::cout << ret << std::endl;
  return 0;
}