Problem:


Score : 200 points

Problem Statement

You have N apples, called Apple 1, Apple 2, Apple 3, ..., Apple N. The flavor of Apple i is L+i-1, which can be negative.

You can make an apple pie using one or more of the apples. The flavor of the apple pie will be the sum of the flavors of the apples used.

You planned to make an apple pie using all of the apples, but being hungry tempts you to eat one of them, which can no longer be used to make the apple pie.

You want to make an apple pie that is as similar as possible to the one that you planned to make. Thus, you will choose the apple to eat so that the flavor of the apple pie made of the remaining N-1 apples will have the smallest possible absolute difference from the flavor of the apple pie made of all the N apples.

Find the flavor of the apple pie made of the remaining N-1 apples when you choose the apple to eat as above.

We can prove that this value is uniquely determined.

Constraints

2 \leq N \leq 200

-100 \leq L \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N L

Output

Find the flavor of the apple pie made of the remaining N-1 apples when you optimally choose the apple to eat.

Sample Input 1

5 2

Sample Output 1

18

The flavors of Apple 1, 2, 3, 4, and 5 are 2, 3, 4, 5, and 6, respectively. The optimal choice is to eat Apple 1, so the answer is 3+4+5+6=18.

Sample Input 2

3 -1

Sample Output 2

0

The flavors of Apple 1, 2, and 3 are -1, 0, and 1, respectively. The optimal choice is to eat Apple 2, so the answer is (-1)+1=0.

Sample Input 3

30 -50

Sample Output 3

-1044



Sample input:
5 2


Sample output:
18

Submission status: Accepted

Solution code:
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
# 一次元累積和
from itertools import accumulate
from bisect import bisect_left, bisect_right

# import numpy as np
# from scipy.special import perm
# from scipy.special import comb

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
INF = float("inf")

########

N, L = l_inpl()
aji = []

for i in range(N):
    aji.append(L+i)

r = INF
for a in aji:
    if abs(r) > abs(a):
        r = a

print(sum(aji)-r)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

from functools import lru_cache

def main(): 
    N, L = LI()
    # abs_min = INF

    li = []
    for i in range(1, N+1):
        li.append((abs(L+i-1), L+i-1))

    li.sort()
    li.pop(0)

    sum_ = 0
    for i in li:
        sum_ += i[1]

    print(sum_)


main()

Submission status: Accepted

Solution code:
N,L = map(int,input().split())
apple = [i+L for i in range(N)]
apple_abs = [abs(i) for i in apple]
del apple[apple_abs.index(min(apple_abs))]
print(sum(apple))

Submission status: Accepted

Solution code:
n,l = map(int,input().split())
s = l*n+n*(n-1)//2
if l <= 0 and l+n-1 >= 0:
    print(s)    
elif l > 0:
    print(s-l)
else:
    print(s-(l+n-1))

Submission status: Accepted

Solution code:
n, l = map(int, input().split())
taste = [i for i in range(l,l+n)]
abs_taste = [abs(t) for t in taste]
min_taste = min(abs_taste)
if min_taste == 0:
	print(sum(taste))
elif min_taste in taste:
	print(sum(taste) - min_taste)
else:
	print(sum(taste) + min_taste)

Submission status: Accepted

Solution code:
#!venv/bin/python
 
import math
import numpy as np
import random
import copy
import statistics 
import bisect
import heapq
from collections import defaultdict 
import time
import sys
from pprint import pprint
import itertools

N, L = [ int(x) for x in input().split() ]
#B = [ int(x) for x in input().split() ]

min_abs = 1000
eat_d = None
sum = 0
for i in range(N):
    d = L + i
    sum += d
    if min_abs > d:
        min_abs = abs(d)
        eat_d = d
print(sum - eat_d)

Submission status: Accepted

Solution code:
n, l = map(int, input().split())
a = []
for i in range(n):
    a.append(l+i)

min_a = 10**10
for i in range(n):
    if abs(a[i]) < abs(min_a):
        min_a = a[i]

print(sum(a) - min_a)

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa == x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))

n,l = m()

k = [l+i for i in range(n)]

co = Max
for i in range(n):
    if abs(co) >= abs(k[i]):
        co = k[i]

print(sum(k) - co)

Submission status: Accepted

Solution code:
n,l=map(int,input().split())
if (l<=0 and l+n-1>=0):
    print(int(n*(l+l+n-1)/2))
elif (l+n-1<=0):
    print(int(n*(l+l+n-1)/2-l-n+1))
else:
    print(int(n*(l+l+n-1)/2-l))

Submission status: Accepted

Solution code:
N, L = map(int, input().split())

if L >= 0:
    last_apple = (L + N - 1)
    apple_total = (L + last_apple) * N // 2
    print(apple_total - L)
else:
    last_apple = (L + N - 1)
    apple_total = (L + last_apple) * N // 2
    if last_apple >= 0:
        print(apple_total)
    else:
        print(apple_total - last_apple)

Submission status: Accepted

Solution code:
N, L = map(int, input().split())
a = [x+L for x in range(N)]
print(sum(a) - min(a, key=lambda x:abs(x)))

Submission status: Accepted

Solution code:
import itertools

def input_ints():
    return list(map(int, input().split()))

def output_list(v):
    print(' '.join(str(x) for x in v))

def main():
    n, l = input_ints()
    a = [x for x in range(l, l + n)]
    ans = sum(a)
    if a[-1] < 0:
        ans -= a[-1]
    if a[0] > 0:
        ans -= a[0]
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,L = map(int,input().split())

ref = 0
for i in range(N):
    ref += L+i
th = 2 ** 30  - 1
for i in range(N):
    tmp = 0
    for j in range(N):
        if i == j:continue
        tmp += L + j
    
    if abs(tmp - ref) < th:
        ans = tmp
        th = abs(tmp-ref)
        
print(ans)

Submission status: Accepted

Solution code:
n,l=map(int,input().split())
a=[l+i for i in range(n)]
b=[abs(l+i) for i in range(n)]
b.sort()
m=b[0]
if not m in a:m*=-1
print(sum(a)-m)

Submission status: Accepted

Solution code:
#    !/usr/bin/env python3
#    encoding: UTF-8
#    Modified: <22/Jun/2019 05:38:20 PM>


#    ✪ H4WK3yE乡
#    Mohd. Farhan Tahir
#    Indian Institute Of Information Technology (IIIT), Gwalior


import sys
import os
from io import IOBase, BytesIO


def main():
    n, l = get_ints()
    arr = [0] * n
    for i in range(n):
        arr[i] = l + i
    arr.sort()
    # print(arr)
    tmp = [(abs(arr[i]), i) for i in range(n)]
    tmp.sort()
    # print(tmp)
    ans = 0
    for i in range(1, n):
        ans += arr[tmp[i][1]]
    print(ans)


BUFSIZE = 8192


class FastIO(BytesIO):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.writable = "x" in file.mode or "w" in file.mode
        self.write = super(FastIO, self).write if self.writable else None

    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
        return s

    def read(self):
        while self._fill():
            pass
        return super(FastIO, self).read()

    def readline(self):
        while self.newlines == 0:
            s = self._fill()
            self.newlines = s.count(b"\n") + (not s)
        self.newlines -= 1
        return super(FastIO, self).readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.getvalue())
            self.truncate(0), self.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        py2 = round(0.5)
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        if py2 == 1:
            self.write = self.buffer.write
            self.read = self.buffer.read
            self.readline = self.buffer.readline
        else:
            self.write = lambda s: self.buffer.write(s.encode("ascii"))
            self.read = lambda: self.buffer.read().decode("ascii")
            self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)


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


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


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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n,l = map(int,input().split())
aji = [ l + i - 1 for i in range(1,n+1) ]
Aji = [ abs(l + i - 1) for i in range(1,n+1) ]
Aji.sort()
try:
    i = aji.index(Aji[0])
except:
    i = aji.index(-Aji[0])
del aji[i]
print(sum(aji))

Submission status: Accepted

Solution code:

def gcd_core(a, b):
    if b == 0:
        return a
    else:
        return gcd_core(b, a % b)


def gcd(arr):
    g = gcd_core(arr[0], arr[1])
    for i in range(2, len(arr)):
        g = gcd_core(g, arr[i])
    return g

n,l = map(int,input().split())
apple = [l+i for i in range(n)]


abm = 100000000000000
index = 0
for i in range(n):
    if abs(apple[i]) < abm:
        index = i
        abm = abs(apple[i])



print(sum(apple)-apple[index])

Submission status: Accepted

Solution code:
p=input().rstrip().split(' ')
N=int(p[0])
L=int(p[1])
l=[]
q=[]
w=[]
S=[]
D=[]
x=0;
for i in range(1,N+1):
    l.append(i)
    q.append(L+i-1)
    x=x+(L+i-1)
for i in range(0,len(q)):
    T=0;
    for j in range(0,len(q)):
        if i!=j:
            T+=q[j]
    S.append(abs(T-x))
    D.append(T)
C=min(S)
print(D[S.index(C)])

Submission status: Accepted

Solution code:

n, l = map(int, input().split())


low = 9999
ans = 0
for i in range(n):
    taste = l+i
    low = min(taste, abs(low))
    ans += taste


print(ans - low)

Submission status: Accepted

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N, L = map(int, input().split())

    ans = 10**4
    total = 0
    for i in range(N):
        total += L + i
        if abs(L+i) <= abs(ans):
            ans = L + i

    return total - ans


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
N,L = map(int,input().split())

a = [0]*N
for i in range(1,N+1):
    a[i-1] = L + i-1

mi = 10*100
k = 0
for i in range(N):
    if mi > abs(a[i]):
        mi = abs(a[i])
        k = i

print(sum(a) - a[k])

Submission status: Accepted

Solution code:
N, L = map(int, input().split())

l = []
ll = []

for i in range(N):
    l.append(L + i)
    ll.append(abs(L + i))
    

ans = sum(l)
a = ll.index(min(ll))
print(ans - l[a])

Submission status: Accepted

Solution code:
N, L = map(int, input().split())
T = [ L + i for i in range(N)]
sumT = sum(T)

if 0 in T:
    print(sumT)
else:
    if T[0] > 0:
        print(sumT - T[0])
    else:
        print(sumT - T[-1])

Submission status: Accepted

Solution code:
n,l = map(int,input().split())

li = [l+i for i in range(n)] 
lia = [abs(l+i) for i in range(n)]
ind = lia.index(min(lia))
print(sum(li)-li[ind])

Submission status: Accepted

Solution code:
N,L = map(int,input().split(" "))
A = [L+x-1 for x in range(1,N+1)]
s = sum(A)
B = [s - A[x] for x in range(N)]
print(min(B,key=lambda x:abs(s-x)))

Submission status: Accepted

Solution code:
N, L = map(int, input().split())
M = L + N - 1

if M < 0:
  print(sum(list(range(L, M))))
elif L > 0:
  print(sum(list(range(L+1, M+1))))
else:
  print(sum(list(range(L, M+1))))

Submission status: Accepted

Solution code:
n,l = map(int,input().split())
a = []
for i in range(n):
  a.append(l+i)
b = list(map(abs,a))
m = min(b)
for i in range(n):
  if abs(a[i])==m:
    del a[i]
    break
print(sum(a))

Submission status: Accepted

Solution code:
N,L = map(int,input().split())

apple = []
for i in range(N):
    apple.append(L+i)
if apple[0] * apple[-1] < 0:
    print(sum(apple))
elif apple[0] < 0:
    print(sum(apple[:N-1]))
else:
    print(sum(apple[1:]))

Submission status: Accepted

Solution code:
N,L = map(int,input().split())

apples = []

abs_min = 10**10
isPlus = True
for i in range(1,N+1):
    now = i+L-1
    apples.append(i+L-1)
    if abs_min > abs(now):
        abs_min = min(abs_min,abs(now))
        if now>=0:
            isPlus = True
        else:
            isPlus = False


if not isPlus:
    abs_min *= -1
    
print(sum(apples) - abs_min)

Submission status: Accepted

Solution code:
n, start = list(map(int, input().split() ))

end = start + n - 1
sumall = (start + end ) * n // 2

ans = -(10**10)
for i in range(n):
	exclude = start + i
	if abs(exclude) < abs(ans - sumall):
		ans = sumall - exclude

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, d=DVSR): return pow(x, d - 2, d)
def DIV(x, y, d=DVSR): return (x * INV(y, d)) % d
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())

N,L=LI()
AJI=[L+i-1 for i in range(1,N+1)]

mi = INTMAX
hikuyatsu = 0
for i in AJI:
    if mi > abs(i):
        mi = abs(i)
        hikuyatsu = i

# print(AJI)
print(sum(AJI)-hikuyatsu)

Submission status: Accepted

Solution code:
N,L=map(int, input().split())

value = L
sum = 0
for i in range(N):
  now = L+i
  sum = sum + now
  if abs(now) < abs(value):
    value = now

print(sum-value)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, L: int):
    S = N * L + N * (N - 1) // 2
    if L <= 0 and N + L -1 >= 0:
        print(S)
    elif L > 0:
        print(S-L)
    else:
        print(S-(N + L - 1))
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    L = int(next(tokens))  # type: int
    solve(N, L)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math 
ni = lambda: int(ns())
na = lambda: list(map(int, input().split()))
ns = lambda: input()
N,L = na()

ans = 10**9
before = 0
t =0
for i in range(N):
    before += L+i
    if ans > abs(L+i):
        ans = min(ans,abs(L+i))
        t = L+i

print(before - t)

Submission status: Accepted

Solution code:
n, l = [int(i) for i in input().split()]
num = 0
a = []
for i in range(n):
  num += i + l
  a.append(i+l)
if(l >= 0):
  ans = num - min(a)
  print(ans)
else:
  if(max(a) >= 0):
    print(num)
  else:
    ans = num - max(a)
    print(ans)

Submission status: Accepted

Solution code:
n,l = map(int,input().split())
if l<=0 and 0<=l+n-1:
    print(n*l+n*(n-1)//2)
elif 0<=l:
    print(n*l+n*(n-1)//2-l)
else:
    print(n*l+n*(n-1)//2-l-n+1)

Submission status: Accepted

Solution code:
N, L = map(int, input().split())
Apples = [L + i - 1 for i in range(1, N + 1)]

ALL_TASTE = sum(Apples)
min_diff = float('inf')
ans = 0
for apple in Apples:
    taste = ALL_TASTE - apple
    if min_diff > abs(ALL_TASTE - taste):
        min_diff = abs(ALL_TASTE - taste)
        ans = taste

print(ans)

Submission status: Accepted

Solution code:
n, l = map(int, input().split())
tastes = [i + l for i in range(n)]
taste_sum = sum(tastes)
t = 0
if tastes[0] > 0:
    t = tastes[0]
elif tastes[-1] < 0:
    t = tastes[-1]
print(taste_sum - t)

Submission status: Accepted

Solution code:
N, L = map(int,input().split())
ans = N*(2*L+N-1)//2
first = L
final = L+N-1
if first * final <= 0:
  print(ans)
else:
  if abs(first) < abs(final):
    print(ans - first)
  else:
    print(ans - final)

Submission status: Accepted

Solution code:
n, l = map(int, input().split())

ans = 0
for i in range(n):
    ans += l + i
if l >= 0:
    print(ans - l)
else:
    if l + n - 1 < 0:
        print(ans - (l + n - 1))
    else:
        print(ans)

Submission status: Accepted

Solution code:
n,l = map(int,input().split())
id = 0
ans = 0
for i in range(n):
    ans+=l+i
    if abs(l+id)>abs(l+i):
        id = i
print(ans-l-id)

Submission status: Accepted

Solution code:
N,L = [ int(it) for it in input().split() ]

li = []
for i in range(N):
  li.append(L+i)
  
su = sum(li)
d = 10000000000000
s = 0
for i in range(N):
  if ( abs( su - li[i] - su)<d ):
    d = abs(su - li[i] - su)
    s = su - li[i]
print ( s )

Submission status: Accepted

Solution code:
N, L = map(int, input().split())
f = 0
for i in range(N):
    f += L + i

memo = 10**9
for i in range(N):
    s = 0
    for j in range(N):
        if j != i:
            s += L + j
    if abs(f - s) < memo:
        memo = abs(f - s)
        res = s
    

print(res)

Submission status: Accepted

Solution code:
N, L = map(int, input().split())

l = []
for i in range(1, N + 1):
	l += [L + i - 1]

l.sort(key=lambda x:abs(x))
#print(l)
print(sum(l[1:]))

Submission status: Accepted

Solution code:
import math

n,l = list(map(int, input().split()))

apple = [l+i for i in range(n)]

if(n+l-1<0):
    print(sum(apple)-apple[-1])
elif(l<=0 and n+l-1>=0):
    print(sum(apple))
else:
    print(sum(apple)-apple[0])

Submission status: Accepted

Solution code:
N,L = [int(i) for i in input().split()]

A = [L+i for i in range(N)]

tmp = 1000
ind = 0
for i in range(N):
    if tmp > abs(A[i]):
        tmp = abs(A[i])
        ind = i
print(sum(A)-A[ind])

Submission status: Accepted

Solution code:
N, L = map(int, input().split())
aji = [L+i for i in range(N)]

choice = abs(aji[0])
m_i = 0
for i, score in enumerate(aji):
    if choice >= abs(score):
        choice = abs(score)
        m_i = i
ans = sum(aji) - aji[m_i]
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env pypy
from __future__ import division, print_function

import os
import sys
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
from io import IOBase

import __pypy__


def main():
    n, l = map(int, input().split())
    tot = n * l + (n * (n - 1) >> 1)

    res, m = 0, float('inf')
    for i in range(n):
        pie = tot - (l + i)
        if abs(pie - tot) < m:
            m = abs(pie - tot)
            res = pie

    print(res)


# region fastio

BUFSIZE = 8192


class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()


class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()


def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n,l = map(int, input().split())

s = 0
for i in range(1,n+1):
    s += l+i-1

ans = 10**6
idx = -1
for i in range(1,n+1):
    s2 = s - (l+i-1)
    if abs(s-s2)<ans:
        ans = abs(s-s2)
        idx = i

print(s-(l+idx-1))

Submission status: Accepted

Solution code:
n, L = map(int, input().split())
a = [L + i - 1 for i in range(1, n + 1)]
ans = 0
eat = float("inf")
for i in a:
    if abs(eat) > abs(i):
        eat = i
print(sum(a) - eat)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


def main():
    n, l = map(int, input().split())
    f = [l + i - 1 for i in range(1, n + 1)]

    print(sum(f) - min(f, key=lambda x: abs(0 - x)))


# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N,L = map(int,input().split())
plan = 0
for i in range(N):
        plan += L + i
diff = 10000000
ans = -1
for i in range(N):
        actual = 0
        for j in range(N):
                if i != j:
                        actual += L + j
        if abs(plan-actual) < diff:
                diff = abs(plan-actual)
                ans = actual
# print(plan, ans, diff)
print(ans)

Submission status: Accepted

Solution code:
N, L = map(int, input().split())
l = [0]*N
for i in range(N):
    l[i] = L+i+1-1
l.sort(key=lambda x: abs(x))
print(sum(l[1:]))

Submission status: Accepted

Solution code:
n,l=map(int,input().split())
su = (2*l+n-1)*n//2
if l>=0:
        print(su-l)
elif l>=-n+1:
        print(su)
else:
        print(su-(l+n-1))

Submission status: Accepted

Solution code:
def inverse(a):
    return pow(a, mod - 2, mod)


def usearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] <= x:
            lft = mid
        else:
            rgt = mid
    return lft


def main():
    n,l=map(int, input().split())
    a = [l+i for i in range(n)]
    ans = 10000
    idx = 0
    for i in range(n):
        if abs(a[i]) < ans:
            ans = abs(a[i])
            idx = i
    print(sum(a) - a[idx])
main()

Submission status: Accepted

Solution code:
# coding: utf-8

import sys
def input(): return sys.stdin.readline().strip()

# N = int(input())
# L = [int(x) for x in input().split()]
N, L = map(int, input().split())
# S = input()

apple = [x + L - 1 for x in range(1, N + 1)]
apple.sort(key=lambda x : abs(x))
print(sum(apple[1:]))

Submission status: Accepted

Solution code:
N, L = map(int, input().split())

A = [L + i - 1 for i in range(1, N+1)]
A = sorted(A, key=lambda x:abs(x))[1:]

print(sum(A))

Submission status: Accepted

Solution code:
n,l = map(int,input().split())

temp1 = [10**9] + [abs(l + i - 1) for i in range(1,n+1)]
temp2 = [10**9] + [l + i - 1 for i in range(1,n+1)]

ans = temp1.index(min(temp1))
print(sum(temp2[1:]) - temp2[ans])

Submission status: Accepted

Solution code:
N,L=map(int,input().split())
ALL=(L+L+N-1)*N//2
if L>=0:
    print(ALL-L)
elif L+N-1<=0:
    print(ALL-(L+N-1))
else:
    print(ALL)

Submission status: Accepted

Solution code:
N, L = map(int, input().split())

a = list(range(L, L + N))
sa = sum(a)
if a[0] * a[-1] <= 0:
    print(sa)
else:
    if a[0] < 0:
        print(sa - a[-1])
    else:
        print(sa - a[0])

Submission status: Accepted

Solution code:
n, l = map(int, input().split())

import numpy as np
taste = np.arange(n)
taste = taste + l

idx = np.argmin(np.abs(taste))

s = 0
for i in range(n):
    if i != idx:
        s += taste[i]

print(s)

Submission status: Accepted

Solution code:
N,L=map(int,input().split())
a=[L+i-1 for i in range(1,N+1)]
mina=min([abs(L+i-1) for i in range(1,N+1)])
if mina not in a:
    mina=-mina
print(sum(a)-mina)

Submission status: Accepted

Solution code:
n, l  =map(int, input().split())

taste = []

for i in range(1, n+1):
    taste.append(l + i - 1)

taste.sort(key=lambda x: abs(x))

print(sum(taste[1:]))

Submission status: Accepted

Solution code:
n,l=map(int,input().split())
hoge=[]
for i in range(n):
  hoge.append(l+i)

  
if(hoge.count(0)==1):
  ans=0
elif(sum(hoge)>0):
  ans=min(hoge)
else:
  ans=max(hoge)

print(sum(hoge)-ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, L):
    t = [L + i-1 for i in range(1, N+1)]
    b = sys.maxsize
    for c in t:
        if abs(c) < abs(b):
            b = c
    return sum(t) - b


def main():
    N, L = read_int_n()
    print(slv(N, L))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, L: int):
    s = N*L+N*(N+1)//2-N
    s -= min([L-1+i for i in range(1, N+1)], key=lambda x: abs(x))
    print(s)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    L = int(next(tokens))  # type: int
    solve(N, L)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)


def solve(N: int, L: int):
    s = L * N + sum(list(range(N + 1))) - N
    mn = float('inf')
    ret = float('inf')
    for i in range(1, N + 1):
        t = L + i - 1
        tmp = abs(s - (s - t))
        if tmp < mn:
            ret = s - t
            mn = tmp
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    L = int(next(tokens))  # type: int
    solve(N, L)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,L = map(int,input().split(' '))

taste = [L+i-1 for i in range(1,N+1)]

if 0 in taste:
	print(sum(taste))
else:
	if taste[0] > 0:
		print(sum(taste[1:]))
	else:
		print(sum(taste[:-1]))

Submission status: Accepted

Solution code:
import sys

def input(): return sys.stdin.readline()[:-1]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def MATINT(h): return [list(map(int, input().split())) for _ in range(h)]
def MATSTR(h): return [input() for _ in range(h)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
inf = float('inf')
mod = 10 ** 9 + 7

def main():
    N, L = MAP()
    aji = 0
    minv = inf
    minv_ = 0
    for i in range(N):
        aji += L + i
        if abs(L + i) < minv:
            minv = abs(L + i)
            minv_ = L + i
    print(aji - minv_)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,L=map(int,input().split())
taste=[L+i-1 for i in range(1,N+1)]
S=sum(taste)
j=0
for i in range(N):
    if abs(taste[j])>abs(taste[i]):
        j=i
print(S-taste[j])