Problem:


Score : 400 points

Problem Statement

One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from his friend.

Find the following:

Constraints

1 ≦ N ≦ 200,000

(a_1, a_2, ..., a_N) is a permutation of (1, 2, ..., N).

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the answer.

Note that the answer may not fit into a 32-bit integer.

Sample Input 1

3
2 1 3

Sample Output 1

9

Sample Input 2

4
1 3 2 4

Sample Output 2

19

Sample Input 3

8
5 4 8 1 2 6 7 3

Sample Output 3

85



Sample input:
3
2 1 3


Sample output:
9

Submission status: Time Limit Exceeded

Solution code:
#root.search(x): ◯.data == x のものが存在するある場合それを出力、ない場合Noneを出力
#root.insert(x): ◯.data == xが存在する場合Falseを出力、ない場合◯.data == x なる頂点を作る
#y.to_s(): 頂点yのdataを出力
#y.left_s(): 頂点yのleftを出力(ない場合はNone)
#root.search_lower(x,None): ○.dataがxより小さいものの中で最も大きいものを出力、ない場合Noneを出力
#root.search_higher(x,None): xより大きいものの中で最も小さいものを出力、ない場合Noneを出力
 
################################################################################
class Avltree:
    def __init__(self,key=None):
        self.key = key
        self.left = None
        self.right = None 
        self.balance = "Even"
    def search(self,key):
        if self.key == None:
            return None
        if self.key > key:
            if self.left == None:
                return None
            else:
                return self.left.search(key)
        if self.key < key:
            if self.right == None:
                return None
            else:
                return self.right.search(key)
        return self # self.key == keyの場合
        
    def search_lower(self,key,key_lower):
        if self.key == None:
            return key_lower
        if self.key > key:
            if self.left == None:
                return key_lower
            else:
                return self.left.search_lower(key,key_lower)
        if self.key < key:
            key_lower = self.key
            if self.right == None:
                return key_lower
            else:
                return self.right.search_lower(key,key_lower)
        #self.key == keyの場合
        if self.left == None:
            return key_lower
        else:
            if key_lower == None:
                return self.left.end_higher(self.left.key)
            else:
                return max(key_lower,self.left.end_higher(self.left.key))
    
    def search_higher(self,key,key_higher):
        if self.key == None:
            return key_higher
        if self.key > key:
            key_higher = self.key
            if self.left == None:
                return key_higher
            else:
                return self.left.search_higher(key,key_higher)
        if self.key < key:
            if self.right == None:
                return key_higher
            else:
                return self.right.search_higher(key,key_higher)
        #self.key == keyの場合
        if self.right == None:
            return key_higher
        else:
            if key_higher == None:
                return self.right.end_lower(self.right.key)
            else:
                return min(key_higher,self.right.end_lower(self.right.key))
        
    
    def end_lower(self,end_lower_key):
        if self.left == None:
            return end_lower_key
        else:
            end_lower_key = self.left.key
            return self.left.end_lower(end_lower_key)
    def end_higher(self,end_higher_key):
        if self.right == None:
            return end_higher_key
        else:
            end_higher_key = self.right.key
            return self.right.end_higher(end_higher_key)
    
            
    def DoubleRightRotation(self):
        tl = self.left
        self.left = tl.right.right
        tl.right.right = self # selfはそのノード
        tlr = tl.right
        tl.right = tlr.left
        tlr.left = tl
        if tlr.balance == "Left":
            tlr.right.balance = "Right"
            tlr.left.balance = "Even"
        elif tlr.balance == "Right":
            tlr.right.balance = "Even"
            tlr.left.balance = "Left"
        elif tlr.balance == "Even":
            tlr.right.balance = "Even"
            tlr.left.balance = "Even"
        tlr.balance = "Even"
        return tlr
    def DoubleLeftRotation(self):
        tr = self.right
        self.right = tr.left.left
        tr.left.left = self
        trl = tr.left
        tr.left = trl.right
        trl.right = tr
        if trl.balance == "Right":
            trl.left.balance = "Left"
            trl.right.balance = "Even"
        elif trl.balance == "Left":
            trl.left.balance = "Even"
            trl.right.balance = "Right"
        elif trl.balance == "Even":
            trl.left.balance = "Even"
            trl.right.balance = "Even"
        trl.balance = "Even"
        return trl
    def SingleLeftRotation(self):
        tr = self.right
        tr.balance = "Even"
        self.balance = "Even"
        self.right = tr.left
        tr.left = self
        return tr
    def SingleRightRotation(self):
        tl = self.left
        tl.balance = "Even"
        self.balance = "Even"
        self.left = tl.right
        tl.right = self
        return tl
    def replace(self,p,v): # 親ノードpの下にある自分(self)をvに置き換える。
        if p.left == self:
            p.left = v
        else :
            p.right = v 
    def insert(self,key): # rootでのみ呼ばれる挿入
        if self.key == None: # rootを含むrotationはしないことにする。
            self.key = key
            return self
        if key < self.key:
            if self.left == None:
                self.left = Avltree(key)
            else:
                self.left.insertx(self,key)
        elif key > self.key:
            if self.right == None:
                self.right = Avltree(key)
            else:
                self.right.insertx(self,key)
        else: # key == self.key:
            pass # do not overwrite
    def insertx(self,p,key): # replaceを呼ぶために一つ上の親を持っているinsert
        if self.key > key:
            if self.left == None:
                self.left = Avltree(key)
            else:
                if not self.left.insertx(self, key): # 左の木が生長しなければ、
                    return False # 成長しない
            if self.balance == "Right":
                self.balance = "Even"
                return False
            elif self.balance == "Even":
                self.balance = "Left"
                return True # 成長した
            elif self.balance == "Left":
                if self.left.balance == "Right":
                    self.replace(p,self.DoubleRightRotation())
                elif self.left.balance == "Left":
                    self.replace(p,self.SingleRightRotation())
                return False # rotationを行うと成長しない
        if self.key < key:
            if self.right == None:
                self.right = Avltree(key)
            else:
                if not self.right.insertx(self, key):
                    return False 
            if self.balance == "Left":
                self.balance = "Even"
                return False
            elif self.balance == "Even":
                self.balance = "Right"
                return True
            elif self.balance == "Right":
                if self.right.balance == "Left": 
                    self.replace(p,self.DoubleLeftRotation())
                elif self.right.balance == "Right":
                    self.replace(p,self.SingleLeftRotation())
                return False
        return False # self.key == keyの時は何もしないので成長もしない 
 
    def to_s(self):
        return self.key
    def left_s(self):
        if self.left == None:
            return None
        return (self.left).key
    def right_s(self):
        if self.right == None:
            return None
        return (self.right).key

################################################################
'''
input = sys.stdin.readline
n = int(input())
a = [int(input()) for _ in range(n)]

#b[i]:iが何番目か
b = [None]*(n+1)
for i in range(n):
    b[a[i]] = i
'''
def inpl(): return [int(i) for i in input().split()]
n = int(input())
a = inpl()
b = {a[i]: i for i in range(n)}

root = Avltree()
ass = 0

for i in range(1,n+1):
    l = root.search_lower(b[i],None)
    r = root.search_higher(b[i],None)
    if l == None:
        l = -1
    if r == None:
        r = n
    ass += (b[i]-l)*(r-b[i])*i
    root.insert(b[i])
print(ass)

Submission status: Time Limit Exceeded

Solution code:
#root.search(x): ◯.data == x のものが存在するある場合それを出力、ない場合Noneを出力
#root.insert(x): ◯.data == xが存在する場合Falseを出力、ない場合◯.data == x なる頂点を作る
#y.to_s(): 頂点yのdataを出力
#y.left_s(): 頂点yのleftを出力(ない場合はNone)
#root.search_lower(x,None): ○.dataがxより小さいものの中で最も大きいものを出力、ない場合Noneを出力
#root.search_higher(x,None): xより大きいものの中で最も小さいものを出力、ない場合Noneを出力

################################################################################
class Avltree:
    def __init__(self,key=None):
        self.key = key
        self.left = None
        self.right = None 
        self.balance = "Even"
    def search(self,key):
        if self.key == None:
            return None
        if self.key > key:
            if self.left == None:
                return None
            else:
                return self.left.search(key)
        if self.key < key:
            if self.right == None:
                return None
            else:
                return self.right.search(key)
        return self # self.key == keyの場合
        
    def search_lower(self,key,key_lower):
        if self.key == None:
            return key_lower
        if self.key > key:
            if self.left == None:
                return key_lower
            else:
                return self.left.search_lower(key,key_lower)
        if self.key < key:
            key_lower = self.key
            if self.right == None:
                return key_lower
            else:
                return self.right.search_lower(key,key_lower)
        #self.key == keyの場合
        if self.left == None:
            return key_lower
        else:
            if key_lower == None:
                return self.left.end_higher(self.left.key)
            else:
                return max(key_lower,self.left.end_higher(self.left.key))
    
    def search_higher(self,key,key_higher):
        if self.key == None:
            return key_higher
        if self.key > key:
            key_higher = self.key
            if self.left == None:
                return key_higher
            else:
                return self.left.search_higher(key,key_higher)
        if self.key < key:
            if self.right == None:
                return key_higher
            else:
                return self.right.search_higher(key,key_higher)
        #self.key == keyの場合
        if self.right == None:
            return key_higher
        else:
            if key_higher == None:
                return self.right.end_lower(self.right.key)
            else:
                return min(key_higher,self.right.end_lower(self.right.key))
        
    
    def end_lower(self,end_lower_key):
        if self.left == None:
            return end_lower_key
        else:
            end_lower_key = self.left.key
            return self.left.end_lower(end_lower_key)
    def end_higher(self,end_higher_key):
        if self.right == None:
            return end_higher_key
        else:
            end_higher_key = self.right.key
            return self.right.end_higher(end_higher_key)
    
            
    def DoubleRightRotation(self):
        tl = self.left
        self.left = tl.right.right
        tl.right.right = self # selfはそのノード
        tlr = tl.right
        tl.right = tlr.left
        tlr.left = tl
        if tlr.balance == "Left":
            tlr.right.balance = "Right"
            tlr.left.balance = "Even"
        elif tlr.balance == "Right":
            tlr.right.balance = "Even"
            tlr.left.balance = "Left"
        elif tlr.balance == "Even":
            tlr.right.balance = "Even"
            tlr.left.balance = "Even"
        tlr.balance = "Even"
        return tlr
    def DoubleLeftRotation(self):
        tr = self.right
        self.right = tr.left.left
        tr.left.left = self
        trl = tr.left
        tr.left = trl.right
        trl.right = tr
        if trl.balance == "Right":
            trl.left.balance = "Left"
            trl.right.balance = "Even"
        elif trl.balance == "Left":
            trl.left.balance = "Even"
            trl.right.balance = "Right"
        elif trl.balance == "Even":
            trl.left.balance = "Even"
            trl.right.balance = "Even"
        trl.balance = "Even"
        return trl
    def SingleLeftRotation(self):
        tr = self.right
        tr.balance = "Even"
        self.balance = "Even"
        self.right = tr.left
        tr.left = self
        return tr
    def SingleRightRotation(self):
        tl = self.left
        tl.balance = "Even"
        self.balance = "Even"
        self.left = tl.right
        tl.right = self
        return tl
    def replace(self,p,v): # 親ノードpの下にある自分(self)をvに置き換える。
        if p.left == self:
            p.left = v
        else :
            p.right = v 
    def insert(self,key): # rootでのみ呼ばれる挿入
        if self.key == None: # rootを含むrotationはしないことにする。
            self.key = key
            return self
        if key < self.key:
            if self.left == None:
                self.left = Avltree(key)
            else:
                self.left.insertx(self,key)
        elif key > self.key:
            if self.right == None:
                self.right = Avltree(key)
            else:
                self.right.insertx(self,key)
        else: # key == self.key:
            pass # do not overwrite
    def insertx(self,p,key): # replaceを呼ぶために一つ上の親を持っているinsert
        if self.key > key:
            if self.left == None:
                self.left = Avltree(key)
            else:
                if not self.left.insertx(self, key): # 左の木が生長しなければ、
                    return False # 成長しない
            if self.balance == "Right":
                self.balance = "Even"
                return False
            elif self.balance == "Even":
                self.balance = "Left"
                return True # 成長した
            elif self.balance == "Left":
                if self.left.balance == "Right":
                    self.replace(p,self.DoubleRightRotation())
                elif self.left.balance == "Left":
                    self.replace(p,self.SingleRightRotation())
                return False # rotationを行うと成長しない
        if self.key < key:
            if self.right == None:
                self.right = Avltree(key)
            else:
                if not self.right.insertx(self, key):
                    return False 
            if self.balance == "Left":
                self.balance = "Even"
                return False
            elif self.balance == "Even":
                self.balance = "Right"
                return True
            elif self.balance == "Right":
                if self.right.balance == "Left": 
                    self.replace(p,self.DoubleLeftRotation())
                elif self.right.balance == "Right":
                    self.replace(p,self.SingleLeftRotation())
                return False
        return False # self.key == keyの時は何もしないので成長もしない 

    def to_s(self):
        return self.key
    def left_s(self):
        if self.left == None:
            return None
        return (self.left).key
    def right_s(self):
        if self.right == None:
            return None
        return (self.right).key

################################################################
n = int(input())
a = list(map(int,input().split()))	

#b[i]:iが何番目か
b = [None]*(n+1)
for i in range(n):
    b[a[i]] = i


root = Avltree()
ass = 0

for i in range(1,n+1):
    l = root.search_lower(b[i],None)
    r = root.search_higher(b[i],None)
    if l == None:
        l = -1
    if r == None:
        r = n
    ass += (b[i]-l)*(r-b[i])*i
    root.insert(b[i])
print(ass)

Submission status: Time Limit Exceeded

Solution code:
#root.search(x): ◯.data == x のものが存在するある場合それを出力、ない場合Noneを出力
#root.insert(x): ◯.data == xが存在する場合Falseを出力、ない場合◯.data == x なる頂点を作る
#y.to_s(): 頂点yのdataを出力
#y.left_s(): 頂点yのleftを出力(ない場合はNone)
#root.search_lower(x,None): ○.dataがxより小さいものの中で最も大きいものを出力、ない場合Noneを出力
#root.search_higher(x,None): xより大きいものの中で最も小さいものを出力、ない場合Noneを出力

################################################################################
class Avltree:
    def __init__(self,key=None):
        self.key = key
        self.left = None
        self.right = None 
        self.balance = "Even"
    def search(self,key):
        if self.key == None:
            return None
        if self.key > key:
            if self.left == None:
                return None
            else:
                return self.left.search(key)
        if self.key < key:
            if self.right == None:
                return None
            else:
                return self.right.search(key)
        return self # self.key == keyの場合
        
    def search_lower(self,key,key_lower):
        if self.key == None:
            return key_lower
        if self.key > key:
            if self.left == None:
                return key_lower
            else:
                return self.left.search_lower(key,key_lower)
        if self.key < key:
            key_lower = self.key
            if self.right == None:
                return key_lower
            else:
                return self.right.search_lower(key,key_lower)
        #self.key == keyの場合
        if self.left == None:
            return key_lower
        else:
            if key_lower == None:
                return self.left.end_higher(self.left.key)
            else:
                return max(key_lower,self.left.end_higher(self.left.key))
    
    def search_higher(self,key,key_higher):
        if self.key == None:
            return key_higher
        if self.key > key:
            key_higher = self.key
            if self.left == None:
                return key_higher
            else:
                return self.left.search_higher(key,key_higher)
        if self.key < key:
            if self.right == None:
                return key_higher
            else:
                return self.right.search_higher(key,key_higher)
        #self.key == keyの場合
        if self.right == None:
            return key_higher
        else:
            if key_higher == None:
                return self.right.end_lower(self.right.key)
            else:
                return min(key_higher,self.right.end_lower(self.right.key))
        
    
    def end_lower(self,end_lower_key):
        if self.left == None:
            return end_lower_key
        else:
            end_lower_key = self.left.key
            return self.left.end_lower(end_lower_key)
    def end_higher(self,end_higher_key):
        if self.right == None:
            return end_higher_key
        else:
            end_higher_key = self.right.key
            return self.right.end_higher(end_higher_key)
    
            
    def DoubleRightRotation(self):
        tl = self.left
        self.left = tl.right.right
        tl.right.right = self # selfはそのノード
        tlr = tl.right
        tl.right = tlr.left
        tlr.left = tl
        if tlr.balance == "Left":
            tlr.right.balance = "Right"
            tlr.left.balance = "Even"
        elif tlr.balance == "Right":
            tlr.right.balance = "Even"
            tlr.left.balance = "Left"
        elif tlr.balance == "Even":
            tlr.right.balance = "Even"
            tlr.left.balance = "Even"
        tlr.balance = "Even"
        return tlr
    def DoubleLeftRotation(self):
        tr = self.right
        self.right = tr.left.left
        tr.left.left = self
        trl = tr.left
        tr.left = trl.right
        trl.right = tr
        if trl.balance == "Right":
            trl.left.balance = "Left"
            trl.right.balance = "Even"
        elif trl.balance == "Left":
            trl.left.balance = "Even"
            trl.right.balance = "Right"
        elif trl.balance == "Even":
            trl.left.balance = "Even"
            trl.right.balance = "Even"
        trl.balance = "Even"
        return trl
    def SingleLeftRotation(self):
        tr = self.right
        tr.balance = "Even"
        self.balance = "Even"
        self.right = tr.left
        tr.left = self
        return tr
    def SingleRightRotation(self):
        tl = self.left
        tl.balance = "Even"
        self.balance = "Even"
        self.left = tl.right
        tl.right = self
        return tl
    def replace(self,p,v): # 親ノードpの下にある自分(self)をvに置き換える。
        if p.left == self:
            p.left = v
        else :
            p.right = v 
    def insert(self,key): # rootでのみ呼ばれる挿入
        if self.key == None: # rootを含むrotationはしないことにする。
            self.key = key
            return self
        if key < self.key:
            if self.left == None:
                self.left = Avltree(key)
            else:
                self.left.insertx(self,key)
        elif key > self.key:
            if self.right == None:
                self.right = Avltree(key)
            else:
                self.right.insertx(self,key)
        else: # key == self.key:
            pass # do not overwrite
    def insertx(self,p,key): # replaceを呼ぶために一つ上の親を持っているinsert
        if self.key > key:
            if self.left == None:
                self.left = Avltree(key)
            else:
                if not self.left.insertx(self, key): # 左の木が生長しなければ、
                    return False # 成長しない
            if self.balance == "Right":
                self.balance = "Even"
                return False
            elif self.balance == "Even":
                self.balance = "Left"
                return True # 成長した
            elif self.balance == "Left":
                if self.left.balance == "Right":
                    self.replace(p,self.DoubleRightRotation())
                elif self.left.balance == "Left":
                    self.replace(p,self.SingleRightRotation())
                return False # rotationを行うと成長しない
        if self.key < key:
            if self.right == None:
                self.right = Avltree(key)
            else:
                if not self.right.insertx(self, key):
                    return False 
            if self.balance == "Left":
                self.balance = "Even"
                return False
            elif self.balance == "Even":
                self.balance = "Right"
                return True
            elif self.balance == "Right":
                if self.right.balance == "Left": 
                    self.replace(p,self.DoubleLeftRotation())
                elif self.right.balance == "Right":
                    self.replace(p,self.SingleLeftRotation())
                return False
        return False # self.key == keyの時は何もしないので成長もしない 

    def to_s(self):
        return self.key
    def left_s(self):
        if self.left == None:
            return None
        return (self.left).key
    def right_s(self):
        if self.right == None:
            return None
        return (self.right).key

################################################################
n = int(input())
a = list(map(int,input().split()))	

#b[i]:iが何番目か
b = [0]*(n+1)
for i in range(n):
    b[a[i]] = i


root = Avltree()
ass = 0

for i in range(1,n+1):
    l = root.search_lower(b[i],None)
    r = root.search_higher(b[i],None)
    if l == None:
        l = -1
    if r == None:
        r = n
    ass += (b[i]-l)*(r-b[i])*i
    root.insert(b[i])
print(ass)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N = int(input())
A = [int(a) for a in input().split()]
Asort = [(A[i], i) for i in range(N)]
Asort.sort()

iniPos = A.index(1)
TotalSum = (iniPos+1) * (N-iniPos)
Position = [A.index(1)]
for i in range(1, N):
    a, posa = Asort[i]
    Left = bisect.bisect_left(Position, posa)
    TotalSum += (posa + 1 if Left == 0 else posa - Position[Left-1]) * (N-posa if Left == a-1 else Position[Left]-posa) * a
    bisect.insort_left(Position, posa)

print(TotalSum)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N = int(input())
A = [int(a) for a in input().split()]

iniPos = A.index(1)
TotalSum = (iniPos+1) * (N-iniPos)
Position = [A.index(1)]
for a in range(2, N+1):
    posa = A.index(a)
    Left = bisect.bisect_left(Position, posa)
    TotalSum += (posa + 1 if Left == 0 else posa - Position[Left-1]) * (N-posa if Left == len(Position) else Position[Left]-posa) * a
    Position.insert(Left, posa)

print(TotalSum)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]
dp = [-1] * (N + 1)
count = [0] * N
for i in range(N):
    count[i] = i - dp[A[i]]
    dp = dp[:A[i]] + [i] * (N - A[i] + 1)
dp_r = [N] * (N + 1)
ans = 0
for i in range(N - 1, -1, -1):
    ans += A[i] * (dp_r[A[i]] - i) * count[i]
    dp_r = dp_r[:A[i]] + [i] * (N - A[i] + 1)
print(ans)

Submission status: Time Limit Exceeded

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

def solve(li):
    if len(li) == 0:
        return 0
    if len(li) == 1:
        return li[0]
    n = len(li)
    k = li.index(min(li))+1
    return k*(n-k+1)*min(li) + solve(li[0:k-1]) + solve(li[k:])

print(solve(a))

Submission status: Time Limit Exceeded

Solution code:
import bisect
def inpl(): return [int(i) for i in input().split()]
N = int(input())
a = inpl()
b = [0 for _ in range(N)]
for i in range(N):
    b[a[i]-1] = i+1
rng = [0, N+1]
ans = 0
for i, v in enumerate(b):
    x = bisect.bisect_left(rng, v)
    st = rng[x-1]
    en = rng[x]
    ans += (i+1)*(v-st)*(en-v)
    bisect.insort_left(rng, v)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
def inpl(): return [int(i) for i in input().split()]
N = int(input())
a = inpl()
b = [0 for _ in range(N)]
for i in range(N):
    b[a[i]-1] = i+1
rng = [0, N+1]
ans = 0
for i, v in enumerate(b):
    x = bisect.bisect_left(rng, v)
    st = rng[x-1]
    en = rng[x]
    ans += (i+1)*(v-st)*(en-v)
    rng = rng[:x] + [v] + rng[x:]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def inpl(): return [int(i) for i in input().split()]
def minisum(x):
    if not x:
        return 0
    mini = x.index(min(x))
    d = len(x)
    if d == 1:
        return x[0]
    result = 0
    if mini > 0:
        result += minisum(x[:mini])
    if mini < d-1:
        result += minisum(x[mini+1:])
    return result + (d-mini)*(mini+1)*x[mini]

N = input()
a = inpl()
print(minisum(a))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(a) for a in input().split()]

s = 0
for i in range(N):
    l = i
    while l>0:
        if A[l-1] > A[i]:
            l -= 1
        else:
            break
    r = i
    while r<N-1:
        if A[r+1] > A[i]:
            r += 1
        else:
            break
    
    s += A[i] * (i-l+1) * (r-i+1)
    
print(s)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
a=list(map(int,input().split()))
b=0
for i in range(len(a)):
    for j in range(i,len(a)):
        b+=min(a[i:j+1])
print(b)

Submission status: Time Limit Exceeded

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

res = 0
def solve(arr):
    global res
    sz = len(arr)
    if sz == 0:
        return
    if sz == 1:
        res += arr[0]
        return
    val = arr[0]
    pivot = 0
    for i,j in enumerate(arr):
        if j < val:
            val = j
            pivot = i
    res += val * (pivot + 1) * (sz - pivot)
    solve(arr[:pivot])
    solve(arr[pivot+1:])

solve(a)
print(res)

Submission status: Time Limit Exceeded

Solution code:
def inpl(): return [int(i) for i in input().split()]
def minisum(x):
    mini = x.index(min(x))
    d = len(x)
    if d == 0:
        return 0
    if d == 1:
        return x[0]
    result = 0
    if mini != 0:
        result += minisum(x[:mini])
    if mini != d-1:
        result += minisum(x[mini+1:])
    return result + (d-mini)*(mini+1)*x[mini]

N = input()
a = inpl()
print(minisum(a))

Submission status: Time Limit Exceeded

Solution code:
def inpl(): return [int(i) for i in input().split()]
def minisum(x):
    mini = x.index(min(x))
    d = len(x)
    if d == 0:
        return 0
    if d == 1:
        return x[0]
    result = 0
    if mini != 0:
        result += minisum(x[:mini])
    if mini != d-1:
        result += minisum(x[mini+1:])
    return result + (d-mini)*(mini+1)*x[mini]

N = input()
a = inpl()
print(minisum(a))

Submission status: Time Limit Exceeded

Solution code:
class BIT_index:
    def __init__(self, n):
        self.BIT_prev = [n] * (n + 1)
        self.BIT_next = [n] * (n + 1)
        self.n = n

    def __addElement(self, BIT, idx):
        value = idx
        idx += 1
        while idx > 0:
            BIT[idx] = min(BIT[idx], value)
            idx -= idx & -idx

    def addElement(self, idx):
        self.__addElement(self.BIT_prev, self.n - 1 - idx)
        self.__addElement(self.BIT_next, idx)

    def __getIndex(self, BIT, idx):
        idx += 1
        ans = self.n
        while idx <= self.n:
            ans = min(ans, BIT[idx])
            idx += idx & -idx
        return ans

    def getIndexPrev(self, idx):
        return self.__getIndex(self.BIT_prev, self.n - 1 - idx)

    def getIndexNext(self, idx):
        return self.__getIndex(self.BIT_next, idx)


N = int(input())
As = list(map(int, input().split()))

Bs = [i for A, i in sorted([(As[i], i) for i in range(N)])]

BIT = BIT_index(N)
ans = 0
for i, B in enumerate(Bs):
    iN = BIT.getIndexNext(B)
    iP = BIT.getIndexPrev(B)
    BIT.addElement(B)
    ans += (i + 1) * (iN - B) * (B - (N - 1 - iP))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import bisect

n = int(input())
a = []
m = [-1 for _ in range(n)]
i = 0
for x in input().split():
    x = int(x)-1
    a.append(x)
    m[x] = i
    i += 1
d = [-1, n]
ans = 0
for i in range(n):
    idx = bisect.bisect(d,m[i])
    lo = m[i] - d[idx-1]
    hi = d[idx] - m[i]
    bisect.insort(d,m[i])
    ans += (i+1)*lo*hi
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import bisect

n = int(input())
a = []
m = [-1 for _ in range(n)]
i = 0
for x in input().split():
    x = int(x)-1
    a.append(x)
    m[x] = i
    i += 1
d = [-1, n]
ans = 0
for i in range(n):
    lo = m[i] - d[bisect.bisect_left(d,m[i])-1]
    hi = d[bisect.bisect_right(d,m[i])] - m[i]
    bisect.insort(d,m[i])
    ans += (i+1)*lo*hi
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def bin_search(queue, num):
    if len(queue) == 0:
        return 0
    l = 0
    r = len(queue)
    while r-l > 1:
        m = (l+r) // 2
        if queue[m] < num:
            l = m
        else:
            r = m
            
    if l == 0 and queue[0] > num:
        r = 0
    return r


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

yyy = 0
for i in range(n):
    a[i] -= 1
    if i > 0 and a[i] > a[i-1]:
        yyy += 1
        
if yyy >= n/2:
    b = []
    for i in range(n):
        b.append(a[n-1-i])
    a = b
        

loc = [-1 for i in range(n)]
for i in range(n):
    loc[a[i]] = i

l = [-1 for i in range(n)]
r = [-1 for i in range(n)]

q = []

for i in range(n):
    ind = bin_search(q, a[i])
    for j in range(ind, len(q)):
        r[q[j]] = i
    if ind ==len(q):
        q.append(a[i])
    else:
        q[ind] = a[i]
    q = q[:ind+1]
    if ind==0:
        l[a[i]] = 0
    else:
        l[a[i]] = loc[q[ind-1]]+1

for i in range(len(q)):
    r[q[i]] = n
    
ans = 0    
for i in range(n):
    a = (i+1) * (loc[i]-l[i]+1) * (r[i]-loc[i])
    ans += a

print(ans)