AtCoder Beginner Contest 139 - ModSum

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-----
2

-----Sample Output-----
1

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

Difficulty: MEDIUM_HARD

Starter code:

Solutions:
n = int(input())
print(n * (n - 1) // 2)


def main():
	N = int(input())
	ans = N * (N - 1) // 2
	print(ans)

def __starting_point():
	main()
__starting_point()


def slove():
	n = int(input())
	print(n * (n - 1) // 2)

def __starting_point():
	slove()
__starting_point()


a = int(input())
print(a * (a - 1) // 2)


from math import gcd
from itertools import combinations, permutations, accumulate, product
from collections import deque, defaultdict, Counter
import decimal
import re
import math
import bisect
import heapq
import sys
sys.setrecursionlimit(10000000)
mod = 998244353
INF = float('inf')
from sys import stdin
readline = stdin.readline

def readInts():
	return list(map(int, readline().split()))

def readTuples():
	return tuple(map(int, readline().split()))

def I():
	return int(readline())
n = I()
print((n - 1) * n // 2)


N = int(input())
nn = N - 1
print(nn * (nn + 1) // 2)


N = int(input())
mod = N * (N - 1) // 2
print(mod)


n = int(input())
print((n * n - n) // 2)


s = int(input())
print(s * (s - 1) // 2)


a = int(input())
print(a * a - a >> 1)


N = int(input())
M = N * (N - 1) // 2
print(M)


N = int(input())
print((0 + N - 1) * N // 2)


n = int(input())
print(sum(range(1, n)))


def delete_head_zeros(n):
	n = str(n)
	l = len(n)
	if '.' in n:
		l = n.find('.')
	head_zeros = 0
	for i in range(l - 1):
		if n[i] == '0':
			head_zeros += 1
		else:
			break
	return n[head_zeros:]

def bigint_compare(a, b):
	a = delete_head_zeros(a)
	b = delete_head_zeros(b)
	if len(a) > len(b):
		return 1
	elif len(a) < len(b):
		return -1
	elif a > b:
		return 1
	elif a < b:
		return -1
	else:
		return 0

def bigint_plus(a, b):
	a = str(a)
	b = str(b)
	d = max([len(a), len(b)])
	a = '0' * (d - len(a)) + a
	b = '0' * (d - len(b)) + b
	ans = ''
	carry = 0
	for i in range(d):
		s = int(a[-i - 1]) + int(b[-i - 1]) + carry
		carry = s // 10
		ans = str(s % 10) + ans
	else:
		if carry:
			ans = str(carry) + ans
	return ans

def bigint_minus(a, b):
	a = str(a)
	b = str(b)
	M = []
	m = []
	sign = ''
	if len(a) > len(b) or (len(a) == len(b) and a >= b):
		[M, m] = [a, b]
	else:
		[M, m] = [b, a]
		sign = '-'
	m = '0' * (len(M) - len(m)) + m
	ans = ''
	borrow = 0
	for i in range(len(M)):
		s = int(M[-i - 1]) - int(m[-i - 1]) - borrow
		if s < 0:
			borrow = 1
			s += 10
		else:
			borrow = 0
		ans = str(s) + ans
	return sign + delete_head_zeros(ans)

def bigint_multiply(a, b):
	a = str(a)
	b = str(b)
	md = []
	for j in range(len(b)):
		carry = 0
		mj = ''
		for i in range(len(a)):
			m = int(a[-i - 1]) * int(b[-j - 1]) + carry
			carry = m // 10
			mj = str(m % 10) + mj
		else:
			if carry:
				mj = str(carry) + mj
		md.append(mj)
	ans = 0
	for k in range(len(md)):
		ans = bigint_plus(md[k] + '0' * k, ans)
	return ans

def bigint_divide(a, b, d=0):
	a = str(a)
	b = str(b)
	d = int(d)
	if d < 0:
		d = 0
	ans = ''
	r = ''
	for i in range(len(a) + d):
		q = 0
		if i < len(a):
			r += a[i]
		elif i == len(a):
			ans += '.'
			r += '0'
		else:
			r += '0'
		if bigint_compare(r, b) == -1:
			ans += str(q)
		else:
			while bigint_compare(r, b) >= 0:
				r = bigint_minus(r, b)
				q += 1
			ans += str(q)
	return delete_head_zeros(ans)

def main():
	N = input()
	print(bigint_divide(bigint_multiply(N, bigint_minus(N, 1)), 2))
main()


n = int(input())
ans = 0
print(n * (n - 1) // 2)


t = int(input())
print(t * (t - 1) // 2)


x = int(input())
print((x - 1) * x // 2)


i = int(input())
print((i - 1) * i // 2)


N = int(input())
S = N * (N - 1) // 2
print(S)


def resolve():
	n = int(input())
	print((n - 1) * n // 2)
resolve()


N = int(input())
ans = int(N * (N - 1) // 2)
if N == 1:
	print(0)
else:
	print(ans)


N = int(input())
ans = (N - 1) * N // 2
print(ans)


3
N = int(input())
ret = N * (N - 1) // 2
print(ret)


n = int(input())
if n == 1:
	print(0)
	return
if n == 2:
	print(1)
	return
if n % 2 == 1:
	print((n - 1 + 1) * ((n - 1) // 2))
else:
	print((n - 1 + 1) * ((n - 1) // 2) + n // 2)


N = int(input())
modsum = (1 + (N - 1)) * (N - 1) // 2
print(modsum)


N = int(input())
print((N ** 2 - N) // 2)


import sys
import heapq
input = sys.stdin.readline
n = int(input())
result = n * (n - 1) // 2
print(result)


N = int(input())
print(N * (N - 1) >> 1)


N = int(input())
print(sum(range(1, N)))


A = int(input())
print(int(A * (A - 1) // 2))


N = int(input().strip())
print(N * (N - 1) // 2)


N = int(input())
if N == 1:
	print(0)
	return
if N % 2 == 0:
	a = N // 2
	ans = (N - 1) * a
else:
	a = (N - 1) // 2
	ans = N * a
print(ans)


n = int(input())
k = (n * n - n) // 2
print(round(k))


a = int(input())
print(sum(range(1, a)))


N = int(input())
SUM = N * (N - 1) // 2
print(SUM)


n = int(input())
ans = (n ** 2 - n) // 2
print(ans)


N = int(input().rstrip())
print(N * (N - 1) // 2)


print(sum(range(int(input()))))


import sys
MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.141592653589793

def read_str():
	return sys.stdin.readline().strip()

def read_int():
	return int(sys.stdin.readline().strip())

def read_ints():
	return map(int, sys.stdin.readline().strip().split())

def read_ints2(x):
	return map(lambda num: int(num) - x, sys.stdin.readline().strip().split())

def read_str_list():
	return list(sys.stdin.readline().strip().split())

def read_int_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def GCD(a: int, b: int) -> int:
	return b if a % b == 0 else GCD(b, a % b)

def LCM(a: int, b: int) -> int:
	return a * b // GCD(a, b)

def Main():
	n = read_int()
	print(n * (n - 1) // 2)

def __starting_point():
	Main()
__starting_point()


n = int(input())
print(n * (n - 1) >> 1)


N = int(input())
print(sum(range(0, N)))


import math
import collections
import itertools

def YesNo(Bool):
	if Bool:
		print('Yes')
	else:
		print('No')
	return

def resolve():
	N = int(input())
	ans = N * (N - 1)
	print(ans // 2)
resolve()


num = int(input())
print((num * num - num) // 2)


n = int(input())
a = 0
a += n * (n - 1) // 2
print(a)


m = int(input())
print(m * (m - 1) // 2)


N = int(input())
c = N * (N - 1) // 2
print(c)


N = int(input())
print(str(N * (N - 1) // 2))


k = int(input())
print(k * (k - 1) // 2)


sigma = lambda x: x * (x + 1) // 2
n = int(input())
print(sigma(n - 1))


X = int(input())
print(X * (X - 1) // 2)


N = int(input())
Answer = N * (N - 1) // 2
print(int(Answer))


print(*[n * (n - 1) // 2 for n in [int(input())]])


N = int(input())
n = N - 1
res = n * (n + 1) // 2
print(res)


N = int(input())
r = N * (N - 1) // 2
print(r)


n = int(input())
total = 0
print(n * (n - 1) // 2)