Problem:


Score : 500 points

Problem Statement

There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.

You can rearrange these children just one time in any order you like.

When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.

Find the maximum total happiness points the children can earn.

Constraints

2 \leq N \leq 2000

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum total happiness points the children can earn.

Sample Input 1

4
1 3 4 2

Sample Output 1

20

If we move the 1-st child from the left to the 3-rd position from the left, the 2-nd child to the 4-th position, the 3-rd child to the 1-st position, and the 4-th child to the 2-nd position, the children earns 1 \times |1-3|+3 \times |2-4|+4 \times |3-1|+2 \times |4-2|=20 happiness points in total.

Sample Input 2

6
5 5 6 1 1 1

Sample Output 2

58

Sample Input 3

6
8 6 9 1 2 1

Sample Output 3

85



Sample input:
4
1 3 4 2


Sample output:
20

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n = int(input())
A = list(map(int,input().split()))
honke = 0
for border in range(n+1):
  ans = 0
  L = []
  for i in range(border):
    L.append((A[i],i))
  L.sort(reverse = True)
  for i in range(border):
    ans += (n-i-1-L[i][1])*L[i][0]


  R = []
  for i in range(border,n):
    R.append((A[i],i))
  R.sort(reverse = True)
  for i in range(n-border):
    ans += (R[i][1]-i)*R[i][0]
  honke = max(honke,ans)
print(honke)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
B = [True]*N
head = 0
tail = N - 1
ans = 0
for i in range(N):
    max_ = -1
    flag_h = True
    for j in range(N):
        if B[j]:
            h = abs(j - head)
            t = abs(j - tail)
            if max(h,t) * A[j] > max_:
                max_ = max(h, t) * A[j]
                flag_h = True if h > t else False
                tmp_ind = j
    ans += max_
    B[tmp_ind] = False
    if flag_h:
        head += 1
    else:
        tail -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
import heapq
import numpy as np
stdin = sys.stdin
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N=int(input())
A=nl()

ans=0
l_infant=[[0]*N for i in range(N)]
for i in range(N):
    for j in range(N):
        l_infant[i][j]=A[i]*abs(j-i)

a_infant=np.array(l_infant)

for i in range(N):
        idx = np.unravel_index(np.argmax(a_infant), a_infant.shape)
        ans += a_infant[idx[0]][idx[1]] 
        a_infant=np.delete(a_infant,idx[0],0)
        a_infant=np.delete(a_infant,idx[1],1)

print(ans)

Submission status: Wrong Answer

Solution code:
def solve(n, aaa):
    aaa_with_idx = list(zip(aaa, range(n)))
    n2 = n // 2
    ans = 0
    for i in range(n):
        left = sorted(aaa_with_idx[i:], reverse=True)
        right = sorted(aaa_with_idx[:i])
        tmp = sum(a * abs(j - k) for j, (a, k) in enumerate(left + right))
        ans = max(ans, tmp)
    return ans


n = int(input())
aaa = list(map(int, input().split()))
print(solve(n, aaa))

Submission status: Wrong Answer

Solution code:
import math
import fractions
import collections
import itertools
import pprint
N=int(input())
A=list(map(int,input().split()))

st=[-1]*N
en=[-1]*N
s=[[0,[0,0]] for _ in range(N*N)]
for i in range(N):
    for j in range(N):
        s[i*N+j][0]=A[i]*abs(i-j)
        s[i*N+j][1][0]=i
        s[i*N+j][1][1] = j
s.sort(key=lambda x:x[0],reverse=True)
#print(s)
cnt=0
for i in range(len(s)):
    a=s[i][1][0]
    b=s[i][1][1]
    c=s[i][0]
    if (st[a]==-1)and(en[b]==-1):
        cnt=cnt+c
        st[a]=0
        en[b]=0
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
a = len(A)
output = 0
index = 0
m = 1
l = a / 2
for i in range(1,a+1) :
    if m + l > a :
        n = m - l
    elif m + l <= a :
        n = m + l
    output +=  A[index] * abs(m-n)
    index += 1
    m += 1
print(output)

Submission status: Wrong Answer

Solution code:

import sys
#import numpy as np

sys.setrecursionlimit(10 ** 9)
#def input():
#    return sys.stdin.readline()[:-1]

N = int(input())
#X, Y = map(int,input().split())
A = list(map(int,input().split()))

left = [0]
right = [0]
ans = 0
for i in range(N):
    l, r = 0, 0
    for j in range(len(left)):
        if A[i] > left[j]:
            buff = i - len(left) + 1 if left[j] == 0 else 1
            l += (A[i] - left[j]) * buff
        else:
            break
    for j in range(len(right)):
        if A[i] > right[j]:
            buff = N-i-1 - len(right) + 1 if right[j] == 0 else 1
            r += (A[i] - right[j]) * buff
        else:
            break
    if r > l:
        right.append(A[i])
        right.sort()
        ans += r
    else:
        left.append(A[i])
        left.sort()
        ans += l
#    print(i,A[i],l,r,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n = int(readline())
lst1 = list(map(int,readline().split()))
"""
lst2 = [[0]*n for _ in range(n)]

for i in range(n): #座標
    for j in range(n): #もの
        lst2[i][j] = abs(i-j)*lst1[j]"""

unused = [1]*n
l = 0
r = n-1
g = 0
ans = 0
while g < n:
    res = -10**9
    idx = None
    if g%2==0:
        for i in range(n):
            if unused[i] and abs(l-i)*lst1[i] >= res:
                if abs(l-i)*lst1[i] == res: #その値が小さい方優先
                    if lst1[i] < lst1[idx]:
                        idx = i
                else:
                    idx = i
                    res = abs(l-i)*lst1[i]
        unused[idx] = 0
        ans += res

        l += 1
    else:
        for i in range(n):
            if unused[i] and abs(r-i)*lst1[i] >= res:
                if abs(l-i)*lst1[i] == res:
                    if lst1[i] < lst1[idx]:
                        idx = i
                else:
                    idx = i
                    res = abs(r-i)*lst1[i]
        unused[idx] = 0
        ans += res

        r -= 1
    
    g += 1
unused = [1]*n
l = 0
r = n-1
g = 0
ans2 = 0
while g < n:
    res = -10**9
    idx = None
    if g%2==1:
        for i in range(n):
            if unused[i] and abs(l-i)*lst1[i] >= res:
                if abs(l-i)*lst1[i] == res: #その値が小さい方優先
                    if lst1[i] < lst1[idx]:
                        idx = i
                else:
                    idx = i
                    res = abs(l-i)*lst1[i]
        unused[idx] = 0
        ans2 += res

        l += 1
    else:
        for i in range(n):
            if unused[i] and abs(r-i)*lst1[i] >= res:
                if abs(l-i)*lst1[i] == res:
                    if lst1[i] < lst1[idx]:
                        idx = i
                else:
                    idx = i
                    res = abs(r-i)*lst1[i]
        unused[idx] = 0
        ans2 += res

        r -= 1
    
    g += 1
print(max(ans,ans2))

Submission status: Wrong Answer

Solution code:
# E - Active Infants

import numpy as np

n = int(input())
A = np.array(list(int(x) for x in input().split()))

D = np.zeros((n, n),dtype=np.int64)

for i in range(n):
    for j in range(n):
        D[i][j] = A[i] * abs(i-j)

ans = 0
for _ in range(n):
    tmp = np.max(D)
    ans += tmp
    y, x = np.unravel_index(np.argmax(D), D.shape)
    D[y, :] = 0
    D[:, x] = 0

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()
sys.setrecursionlimit(10**6)

n = ni()
a = na()
b = []
r = [0]
for i in range(n):
    b.append((a[i],i))
for i in a:
    r.append(r[-1]+i)

b.sort(reverse=True)

ans = 0

def f(left,right,k,c):
    if k == n:
        return c
    i,j = b[k]
    x = abs(j-left)
    y = abs(right-j)
    xx = (left,j+1) if j>left else (j,left+1)
    yy = (right,j+1) if j>right else (j,right+1)
    p = r[xx[1]] - r[xx[0]]
    q = r[yy[1]] - r[yy[0]]
    if x+p > y+q:
        return f(left+1,right,k+1,c+x*i)
    elif x+p < y+q:
        return f(left,right-1,k+1,c+y*i)
    return max(f(left+1,right,k+1,c+x*i), f(left,right-1,k+1,c+y*i))

ans = f(0,n-1,0,0)

print(ans)

Submission status: Wrong Answer

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

from collections import deque

def solve():
    n = II()
    A = [(idx+1, j) for idx, j in enumerate(MI())]
    d = deque([i+1 for i in range(n)])
    B = sorted(A, key=lambda x:x[1], reverse=True)
    dct = {}
    for idx, j in B:
        dct[idx] = j

    # print(dct)
    # print(d)
    harf = n // 2
    ans = 0
    # for b, idx in B:
    for j in range(n):
        idx, b = B[j]
        if idx <= harf:
            p = d.pop()
        else:
            p = d.popleft()
        # print(idx, b, p, abs(idx - p))
        ans = ans + b * abs(idx - p)
        # d.remove(idx)
        # print(d)
    print(ans)





if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
n=int(input())
ls=list(map(int,input().split()))
d={}
for i in range(n):
    d[i]=ls[i]
start_i=0
end_i=n-1
sums=0
left_sum=0
right_sum=sum(ls)
for i in range(n):
    mx=0
    mx_pair=[]
    for idx,infant in d.items():
        if infant==-1:continue
        left_sum+=infant
        right_sum-=infant
        left, right = infant*(abs(idx-start_i)), infant*(abs(idx-end_i))
        if left>=mx:
            mx=left
            mx_pair=[idx,"left"]
        if right>=mx:
            mx=right
            mx_pair=[idx,"right"]
    sums+=mx
    if mx_pair[1]=="left":
        start_i+=1
    elif mx_pair[1]=="right":
        end_i-=1
    d[mx_pair[0]]=-1
print(sums)

Submission status: Wrong Answer

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

left = 0
right = len(a) - 1
ans = 0

for i in range(n):
    # print(right, left)
    tmp = 0
    num = 0
    l, r = False, False
    for j in range(len(a)):
        if abs(j - right) > abs(j - left):
            if tmp <= abs(j - right) * a[j]:
                tmp = abs(j - right) * a[j]
                num = j    
                l, r = False, True

        elif abs(j - right) < abs(j - left):
            if tmp <= abs(j - left) * a[j]:
                tmp = abs(j - left) * a[j]
                num = j    
                l, r = True, False

        else:
            if tmp <= abs(j - left) * a[j]:
                for k in range(len(a)):
                    tl, tr = False, False
                    te = 0
                    if k != j:
                        if abs(k - right) > abs(j - left):
                            if te < abs(k - right) * a[k]:
                                te = abs(k - right) * a[k]
                                tl, tr = False, True
                        else:
                            if te < abs(k - left) * a[k]:
                                te = abs(k - left) * a[k]
                                tl, tr = True, False
                num = j
                if tl:
                    l, r = False, True
                    tmp += abs(j - right) * a[j]
                else:
                    l, r = True, False
                    tmp += abs(j - left) * a[j]
                
    ans += tmp    
    a[num] = 0
   
    if l:
        left += 1
    if r:
        right -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
n=int(input())
A=list(map(int,input().split()))
index=np.argsort(A)[::-1]
under=0
top=n-1
point=[0]*n
for i in range(n-1):
	j=index[i]
	if top-j>j-under:
		point[j]=top
		top-=1
	elif top-j<j-under:
		point[j]=under
		under+=1
	else:
		if j>index[i+1]:
			point[j]=under
			under+=1
		else:
			point[j]=top
			top-=1
point[index[-1]]=top
ans=0
for i in range(n):
	ans+=A[i]*abs(point[i]-i)
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[]
for i,a in enumerate(map(int,input().split())):
  A.append([a,i])
A=sorted(A,key=lambda x:x[0], reverse=True)
r,l=0,N-1
ans=0
i=0
while r<=l:
  _=[A[i]]
  i+=1
  while i<N and A[i][0]==_[0][0]:
    _.append(A[i])
    i+=1
  _=sorted(_,key=lambda x:abs((l+r)//2-x[1]),reverse=True)
  for a,j in _:
    if (l+r)/2<j:
      ans+=a*(j-r)
      r+=1
    else:
      ans+=a*(l-j)
      l-=1
print(ans)

Submission status: Wrong Answer

Solution code:
#ここまでテンプレ
from sys import stdin
from sys import setrecursionlimit

n = int(stdin.readline().rstrip())
a = list(map(int,stdin.readline().rstrip().split()))

ma = 0

#i人分ずらす
for i in range(n):
    #j番目の人に対する操作
    point = 0
    for j in range(n):
        iti = min(i,abs(j-(j+i)%n))
        point += a[j]*iti
    ma = max(ma,point)
    
print(ma)

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
a = list(map(int, input().split()))
newa = []
for i, j in enumerate(a, 0):
    newa.append([i, j])
newa.sort(key=lambda x:x[1], reverse=True)
ans_list = deque(list(range(n)))
ans = 0
for index, value in newa:
    if abs(index-ans_list[0]) >= abs(index-ans_list[-1]):
        res = ans_list.popleft()
        ans += value*abs(index-res)
    else:
        res = ans_list.pop()
        ans += value*abs(index-res)    
    print(res, value*abs(index-res))
print(ans+1)

Submission status: Wrong Answer

Solution code:
#mod = 10**9 + 7
import heapq
############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
def inp():
    return(int(input().rstrip()))
def inlt():
    return(list(map(int,input().rstrip().split())))
def insr():
    s = input().rstrip()
    return(s[:len(s) - 1])
def invr():
    return(map(int,input().rstrip().split()))
################################################################
n = inp()
nums = inlt()
nums = [[nums[i],i] for i in range(n)]
ans = 0
left = 0
right = n - 1
while left < right:
    the_max = 0
    ri = left
    for i in range(left,right + 1):
        val,index = nums[i]
        if abs(right - index)*val > the_max:
            the_max = abs(right - index)*val
            ri = i
    the_m = 0
    for i in range(right,left - 1,-1):
        val,index = nums[i]
        if abs(index - left)*val > the_m:
            the_m = abs(index - left)*val
            li = i
    if the_max > the_m:
        nums[ri][0] = nums[right][0]
        nums[ri][1] = nums[right][1]
        right -= 1
        ans += the_max
    else:
        nums[li][0] = nums[right][0]
        nums[li][1] = nums[right][1]
        left += 1
        ans += the_m
    #print(ans)

print(ans + 2)  
#print(n - s if s <= n else -1)
#print('No')
#print()

Submission status: Wrong Answer

Solution code:
def solve(n, aaa):
    aaa_with_idx = list(zip(aaa, range(n)))
    n2 = n // 2
    ans = 0
    for i in range(n):
        if i <= n2:
            srt = sorted(aaa_with_idx[i:], reverse=True)
            left = srt[:n2]
            right = sorted(srt[n2:] + aaa_with_idx[:i])
        else:
            srt = sorted(aaa_with_idx[:i])
            right = srt[-n2:]
            left = sorted(srt[:-n2] + aaa_with_idx[i:], reverse=True)

        tmp = sum(a * abs(j - k) for j, (a, k) in enumerate(left + right))
        ans = max(ans, tmp)
    return ans


n = int(input())
aaa = list(map(int, input().split()))
print(solve(n, aaa))

Submission status: Wrong Answer

Solution code:
N = int(input())
l = list(map(int, input().split()))
s = 0
for i in range(int(N/2)):
    d = 0
    b = N
    for j in range(N):
        if l[j]*abs(i-j) >= d:
            d = l[j]*abs(i-j)
            b = j
    s += d
    l[b]= 0
    d = 0
    b = N
    for j in range(N):
        if l[j]*abs(N-1-i-j) >= d:
            d = l[j]*abs(N-1-i-j)
            b = j
    s += d
    l[b]= 0
if N%2 == 1:
    d = 0
    b = N
    for j in range(N):
        if l[j]*abs(int((N-1)/2)-j) >= d:
            d = l[j]*abs(int((N-1)/2)-j)
            b = j
    s += d
print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
a = list(map(int, input().split()))
newa = []
for i, j in enumerate(a, 0):
    newa.append([i, j])
newa.sort(key=lambda x:x[1], reverse=True)
ans_list = deque(list(range(n)))
ans = 0
for index, value in newa:
    if abs(index-ans_list[0]) >= abs(index-ans_list[-1]):
        res = ans_list.popleft()
        ans += value*abs(index-res)
    else:
        res = ans_list.pop()
        ans += value*abs(index-res)    
    print(res, value*abs(index-res))
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import heapq
n = int(input())
a = np.array(list(map(int, input().split())))
check = [-1] * n
ans = 0
for u in range(n//2):
    t = np.array([abs(i-u) for i in range(n)])
    r = a * t
    index = np.where(r == np.max(r))[0][-1]
    ans += r[index]
    a[index] = 0

for u in reversed(range(n//2, n)):
    t = np.array([abs(i - u) for i in range(n)])
    r = a * t
    index = np.where(r == np.max(r))[0][-1]
    ans += r[index]
    a[index] = 0

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
Atmp=list(map(int,input().split(' ')))
A=[]
for i in range(N):
    A.append([Atmp[i],i])

Afin=[]
if N%2 == 0:
    Aleft=A[0:N//2]
    Aleft.sort()
    Aright=A[N//2:N]
    Aright.sort(reverse=True)

    Afin = Aright
    Afin.extend(Aleft)
    cnt=0
    for i in range(N):
        cnt+=Afin[i][0]*abs(Afin[i][1]-i)
    print(cnt)
else:
    Aleft=A[0:int(N//2)]
    Aleft.sort()
    Aright=A[int(N//2):N]
    Aright.sort(reverse=True)

    Afin = Aright
    Afin.extend(Aleft)
    cnt1=0
    for i in range(N):
        cnt1+=Afin[i][0]*abs(Afin[i][1]-i)

    Aleft=A[0:int(N//2)+1]
    Aleft.sort()
    Aright=A[int(N//2)+1:N]
    Aright.sort(reverse=True)

    Afin = Aright
    Afin.extend(Aleft)
    cnt2=0
    for i in range(N):
        cnt2+=Afin[i][0]*abs(Afin[i][1]-i)

    print(max(cnt1,cnt2))

Submission status: Wrong Answer

Solution code:
n=int(input())
ls=list(map(int,input().split()))
d={}
for i in range(n):
    d[i]=ls[i]
start_i=0
end_i=n-1
sums=0
left_sum=0
right_sum=sum(ls)
for i in range(n):
    mx=0
    mx_pair=[]
    for idx,infant in d.items():
        if infant==-1:continue
        left_sum+=infant
        right_sum-=infant
        left, right = infant*(abs(idx-start_i)), infant*(abs(idx-end_i))
        if left==right and left>=mx:
            if left_sum>=right_sum:
                mx_pair=[idx,"left"]
            else:
                mx_pair=[idx,"right"]
            continue
        if left>=mx:
            mx=left
            mx_pair=[idx,"left"]
        if right>=mx:
            mx=right
            mx_pair=[idx,"right"]
    sums+=mx
    if mx_pair[1]=="left":
        start_i+=1
    elif mx_pair[1]=="right":
        end_i-=1
    d[mx_pair[0]]=-1
print(sums)

Submission status: Wrong Answer

Solution code:
import math
import fractions
import collections
import itertools
import pprint
N=int(input())
A=list(map(int,input().split()))

st=[-1]*N
en=[-1]*N
s=[[0,[0,0]] for _ in range(N*N)]
for i in range(N):
    for j in range(N):
        s[i*N+j][0]=A[i]*abs(i-j)
        s[i*N+j][1][0]=i
        s[i*N+j][1][1] = j
s.sort(key=lambda x:x[0],reverse=True)
#print(s)
cnt=0
for i in range(len(s)):
    if (st[s[i][1][0]]==-1)and(en[s[i][1][1]]==-1):
        cnt=cnt+s[i][0]
        st[s[i][1][0]]=0
        en[s[i][1][1]]=0
print(cnt)

Submission status: Wrong Answer

Solution code:
import random
import time
def LI():return list(map(int,input().split()))
start=time.time()
n=int(input()) # 2000 O(nn?),O(nlogn)
l=[i for i in range(n)]
a=LI()
ans=0
while True:
    if time.time()-start>1.60:
        print(ans)
        exit()
    random.shuffle(l)
    tmp=0
    for i,(j,k) in enumerate(zip(a,l)):
        if i==k:continue
        tmp+=j*abs(i-k)
    ans=max(ans,tmp)

Submission status: Wrong Answer

Solution code:
N = int(input())
As = [int(s) for s in input().split()]
rAs = list(reversed(As))

def f(As):
    l = 0
    r = N - 1
     
    S = 0
    for k in range(N):
        m, mp, il = 0, 0, False
        for p in range(N):
            v = As[p]
     
            tm = v * abs(l - p)
            if m < tm:
                m = tm
                mp = p
                il = True
     
            tm = v * abs(r - p)
            if m < tm:
                m = tm
                mp = p
                il = False
     
        S += m
        As[mp] = 0
        if il:
            l += 1
        else:
            r -= 1

    return S

print(max(f(As), f(rAs)))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
flag = [1 for i in range(N)]
left = 0
right = N - 1
ans = 0
for i in range(N):
    index = 0
    MAX = 0
    for j in range(N):
        if flag[j] and MAX < A[j]:
            MAX = A[j]
            index = j
    if sum(A[:index]) < sum(A[index+1:]):
        ans += A[index] * abs(right - index)
        right -= 1
    else:
        ans += A[index] * abs(left - index)
        left += 1
    flag[index] = 0
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
l=[0]
ure=0
s=[]
for i in range(2,N+1):#i人使う
  loc=0
  ure=A[i-1]*(i-1)#ok
  #print()
  for j in range(i-1):#ok
    ure+=A[l[j]]*abs(l[j]-(j+1))#
  #print(ure)
  for j in range(i-1):#i+1個の入る場所
    if(ure<=ure-A[i-1]*(j+1)-A[l[j]]*abs(l[j]-(j+1))+A[l[j]]*abs(l[j]-j)):
      loc=j+1
      ure=ure-A[i-1]*(j+1)-A[l[j]]*abs(l[j]-(j+1))+A[l[j]]*abs(l[j]-j)
  ss=l[:]
  l=l[:loc]
  l.append(i-1)
  l.extend(ss[loc:])
  #print(l)
  #print("owari")
print(ure+1)

Submission status: Wrong Answer

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

AA=[]
for i in range(N):
    AA.append([A[i],i])
AA.sort(reverse=True)

f=0
l=N-1
ans=0

for j in range(N):
    if abs(AA[j][1]-f)>abs(l-AA[j][1]):
        ans+=AA[j][0]*abs(AA[j][1]-f)
        f+=1
    else:
        ans+=AA[j][0]*abs(l-AA[j][1])
        l-=1
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import *
import sys
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import time
import math
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
N=int(input())
A=list(map(int,input().split()))
A=[[A[i],i] for i in range(N)]
A.sort()
c=0
for i in range(N//2):
    a1,i1=A.pop()
    a2,i2=A.pop()
    b=(N-1-i)
    c+=max(a1*round(abs(i-i1))+a2*round(abs(b-i2)),a1*round(abs(b-i1))+a2*round(abs(i-i2)))
if N%2:
    a1,i1=A.pop()
    b=(N-1)//2
    c+=a1*round(abs(b-i1))
print(c)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[]
for i,a in enumerate(map(int,input().split())):
  A.append([a,i])
A=sorted(A,key=lambda x:x[0], reverse=True)
r,l=0,N-1
ans=0
for a,i in A:
  if (l+r)/2<i:
    ans+=a*(i-r)
    r+=1
  else:
    ans+=a*(l-i)
    l-=1
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[]
for i,a in enumerate(map(int,input().split())):
  A.append([a,i])
A=sorted(A,key=lambda x:x[0], reverse=True)
r,l=0,N-1
ans=0
for a,i in A:
  if (l+r)//2<i:
    ans+=a*(i-r)
    r+=1
  else:
    ans+=a*(l-i)
    l-=1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n = int(readline())
lst1 = list(map(int,readline().split()))
"""
lst2 = [[0]*n for _ in range(n)]

for i in range(n): #座標
    for j in range(n): #もの
        lst2[i][j] = abs(i-j)*lst1[j]"""

unused = [1]*n
l = 0
r = n-1
g = 0
ans = 0
while g < n:
    res = -10**9
    idx = None
    if g%2==0:
        for i in range(n):
            if unused[i] and abs(l-i)*lst1[i] >= res:
                if abs(l-i)*lst1[i] == res: #その値が小さい方優先
                    if lst1[i] < lst1[idx]:
                        idx = i
                else:
                    idx = i
                    res = abs(l-i)*lst1[i]
        unused[idx] = 0
        ans += res

        l += 1
    else:
        for i in range(n):
            if unused[i] and abs(r-i)*lst1[i] >= res:
                if abs(l-i)*lst1[i] == res:
                    if lst1[i] < lst1[idx]:
                        idx = i
                else:
                    idx = i
                    res = abs(r-i)*lst1[i]
        unused[idx] = 0
        ans += res

        r -= 1
    
    g += 1

print(ans)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

def get_new_position(old_index):
    global new_positions
    min_pos = new_positions[0]
    max_pos = new_positions[len(new_positions) - 1]
    if abs(min_pos - old_index) > abs(max_pos - old_index):
        return new_positions.pop(0)
    else:
        return new_positions.pop(len(new_positions) - 1)

numbers = int(input())
infants = list(map(lambda x: int(x), input().split(' ')))

data = [[0, 0] for i in range(numbers)]
for i, infant in enumerate(infants):
    data[i] = [i, infant]

data.sort(key=itemgetter(1), reverse=True)

new_positions = [i for i in range(numbers)]

point = 0
for infant in data:
    point += (abs(infant[0] - get_new_position(infant[0])) * infant[1])

print(point)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = sum(a)
print(b * n // 2)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = []
for i in range(n):
    b.append([a[i],i])
b.sort()
b = b[::-1]
print(b)
dp = [[0]* (n+1) for i in range(n+1)]
cnt = 0
for i,j in b:
    for k in range(cnt+1):
        dp[cnt-k][k+1] = max(dp[cnt-k][k] + abs(n-1-j-k)*i,dp[cnt-k][k+1])
        dp[cnt-k+1][k] = max(dp[cnt-k][k] + abs(j-(cnt-k))*i,dp[cnt-k+1][k])
    cnt+=1
ans = 0
for k in range(n):
    ans = max(ans,dp[n+1-k-1][k])
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[]
for i,a in enumerate(map(int,input().split())):
  A.append([a,i])
A=sorted(A,key=lambda x:x[0], reverse=True)
r,l=0,N-1
ans=0
i=0
while r<=l:
  _=[A[i]]
  i+=1
  while i<N and A[i][0]==_[0][0]:
    _.append(A[i])
    i+=1
  _=sorted(_,key=lambda x:abs((l+r)//2-x[1]),reverse=True)
  for a,j in _:
    if (l+r)//2<j:
      ans+=a*(j-r)
      r+=1
    else:
      ans+=a*(l-j)
      l-=1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
A = sorted([(A[i],i) for i in range(N)],key=lambda x:x[0],reverse=True)
curl = 0
curr = N-1
cnt = 0
for i in range(N):
    a,ind = A[i]
    if abs(curr-ind)>=abs(curl-ind):
        cnt += a*abs(curr-ind)
        curr -= 1
    else:
        cnt += a*abs(curl-ind)
        curl += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import sys,bisect,string,math,time,functools,random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[1]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[1]+[mp_def[j] for j in s]+[1]
    mp+=[1]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)];rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()

class Comb:
    def __init__(self,n,mo=10**9+7):
        self.fac=[0]*(n+1)
        self.inv=[1]*(n+1)
        self.fac[0]=1
        self.fact(n)
        for i in range(1,n+1):
            self.fac[i]=i*self.fac[i-1]%mo
            self.inv[n]*=i
            self.inv[n]%=mo
        self.inv[n]=pow(self.inv[n],mo-2,mo)
        for i in range(1,n):
            self.inv[n-i]=self.inv[n-i+1]*(n-i+1)%mo
        return
    
    def fact(self,n):
        return self.fac[n]
        
    def invf(self,n):
        return self.inv[n]

    def comb(self,x,y):
        if y<0 or y>x:
            return 0
        return self.fac[x]*self.inv[x-y]*self.inv[y]%mo

show_flg=False
#show_flg=True

ans=0


n=I()
A=LI()
a=list(enumerate(A))
a.sort(key=lambda x:x[1])
show('a',a)

b=[[]for i in range(n)]
for i in range(n):
    for j in range(n):
        b[i].append(abs(i-j)*A[i])

for j in b:
    show('b',j)

l,r=0,n-1
rng=list(range(n))
skp=set()
for i in range(n):
    mx_l,mx_r=-1,-1
    for j in range(n):
        if j in skp:continue
        if b[j][l]>mx_l:
            mx_l=b[j][l]
            l_can=set()
            l_can.add(j)
        elif b[j][l]==mx_l:
            l_can.add(j)

        if b[j][r]>mx_r:
            mx_r=b[j][r]
            r_can=set()
            r_can.add(j)
        elif b[j][r]==mx_r:
            r_can.add(j)
    show('mx_l,mx_r',mx_l,mx_r)
    if mx_r>mx_l:
        ans+=mx_r
        r-=1
        mn_l=1<<63
        for x in r_can:
            if b[x][l]<mn_l:
                mn_l=b[x][l]
                r_skp=x
        skp.add(r_skp)
        show('r')
    elif mx_l>mx_r:
        ans+=mx_l
        l+=1
        mn_r=1<<63
        for x in l_can:
            if b[x][r]<mn_r:
                mn_r=b[x][r]
                l_skp=x
        skp.add(l_skp)
        show('l')
    elif mx_l==mx_r:
        ans+=mx_l
        bt_l=-1
        bt_r=-1

        mn_r=1<<63
        for x in l_can:
            if b[x][r]<mn_r:
                mn_r=b[x][r]
                l_skp=x
        mn_l=1<<63
        for x in r_can:
            if b[x][l]<mn_l:
                mn_l=b[x][l]
                r_skp=x

        for x in l_can:
            if b[x][r]<bt_r:
                bt_r=b[x][r]
                bt_ri=x
        for x in r_can:
            if b[x][l]<bt_l:
                bt_l=b[x][l]
                bt_li=x
        if bt_r>bt_l:
            l+=1
            skp.add(l_skp)
        else:
            r-=1
            skp.add(r_skp)
        
        
        show('=')
    show(skp,(l,r),ans)

print(ans)