Problem:


Score : 300 points

Problem Statement

In a flower bed, there are N flowers, numbered 1,2,......,N. Initially, the heights of all flowers are 0.
You are given a sequence h=\{h_1,h_2,h_3,......\} as input. You would like to change the height of Flower k to h_k for all k (1 \leq  k \leq N), by repeating the following "watering" operation:

Specify integers l and r. Increase the height of Flower x by 1 for all x such that l \leq x \leq r.

Find the minimum number of watering operations required to satisfy the condition.

Constraints

1 \leq N  \leq 100

0 \leq h_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 h_3 ...... h_N

Output

Print the minimum number of watering operations required to satisfy the condition.

Sample Input 1

4
1 2 2 1

Sample Output 1

2

The minimum number of watering operations required is 2.
One way to achieve it is:

Perform the operation with (l,r)=(1,3).

Perform the operation with (l,r)=(2,4).

Sample Input 2

5
3 1 2 3 1

Sample Output 2

5

Sample Input 3

8
4 23 75 0 23 96 50 100

Sample Output 3

221



Sample input:
4
1 2 2 1


Sample output:
2

Submission status: Accepted

Solution code:
n = int(input())
H = list(map(int,input().split()))
ans = 0
while H.count(0) != len(H):
  l,r = -1,-1
  for i in range(len(H)):
    if l == -1 and H[i] != 0:
      l = i
      r = l
    elif H[i] != 0:
      r = i
    elif l != -1 and H[i] == 0:
      break
  selected = [ h - 1 for h in H[l:r+1]]
  H[l:r+1] = selected
  ans += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int,input().split()))
count = 0

def devide(h):
    global count
    while 0 not in h:
        for i in range(len(h)):
            h[i] -= 1
        count += 1
    h2 = []
    h3 = []
    for i in h:
        if i == 0:
            h2.append(h3)
            h3 = []
        if i != 0:
            h3.append(i)
    h2.append(h3)

    for i in h2:
        if i == []:
            continue
        else:
            devide(i)
            
devide(h)
print(count)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int,input().split()))
count = 0
def island(h):
    global count
    while 0 not in h:
        for i in range(len(h)):
            h[i] -= 1
        count += 1

def devide(h):
    h2 = []
    h3 = []
    for i in h:
        if i == 0:
            h2.append(h3)
            h3 = []
        if i != 0:
            h3.append(i)
    h2.append(h3)


    for i in h2:
        if i == []:
            continue
        else:
            island(i)
            devide(i)
            
island(h)
devide(h)
print(count)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
h0 = [int(i) for i in input().split()]
h = np.zeros(n,dtype = int)
x = np.zeros(n,dtype = int)
#最小値
def minh(h,x):
  tmp1 = 100
  for i in range(n):
    if x[i] == 0:
      if h0[i] <= tmp1:
        tmp1 = h0[i]
  return tmp1
#水やり
def mizu(h,x,tmp):
  for i in range(n):
    if x[i] == 0:
      h[i] = tmp
    if h0[i] == tmp:
      x[i] = 1
#分割数
def bunnkatu(x):
  for i in range(n):
    if i == 0:
      if x[i] == 0:
        s = 1
      else:
        s = 0
    else:
      if x[i] == 0 and x[i-1] == 1:
        s += 1
  return s

def main():
  tmp = min(h0)
  ans = tmp
  mizu(h,x,tmp)
  while tmp < max(h0):
    tmp0 = minh(h,x)
    s = bunnkatu(x)
    mizu(h,x,tmp0)
    ans += s*(tmp0 - tmp)
    tmp = tmp0
  return ans

ans = main()
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
amin = min(a)
before = 0
ans = amin
count = 0
for i in range(n):
    now = a[i]-amin
    if before - now < 0:
        count += abs(before - now)
    before = now
print(ans+count)

Submission status: Accepted

Solution code:
def find_index(l, x):
    return l.index(x) if x in l else -1

def water(sum, H):
    if len(H)==0:
        return sum
    elif len(H)==1:
        sum += H[0]
        return sum
    minimum = min(H)
    sum += minimum
    for i in range(len(H)):
        H[i] -= minimum
    index = find_index(H, 0)
    H1 = H[:index]
    sum = water(sum,H1)
    H2 = H[index+1:]
    sum = water(sum,H2)
    return sum


    water(sum, array)

def main():
    n = int(input())
    H = list(map(int, input().split()))
    sum = 0
    sum += water(sum, H)
    print(sum)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n = ri()
ls = ril()

res = 0
for x, y in zip([0] + ls, ls):
    if x < y:
        res += y - x
print(res)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n = ri()
ls = ril()

prev = 0
up = True
res = 0
for x, y in zip(ls, ls[1:] + [0]):
    if up and x > y:
        res += x - prev
        up = False
    elif not up and x < y:
        prev = x
        up = True
print(res)

Submission status: Accepted

Solution code:
from collections import Counter
import sys

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

res = 0

while True:
    if max(Hs) == 0:
        break

    i = 0
    while i < n:
        if Hs[i] == 0:
            i += 1
            continue
        else:
            res += 1
            while i < n and Hs[i] > 0:
                Hs[i] -= 1
                i += 1

print(res)

Submission status: Accepted

Solution code:
N = int(input())
base=0
ans=0
for h in list(map(int,input().split())):
    ans+=max(0,h-base)
    base=h
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
ls=[int(s) for s in input().split()]
ls.append(0)
a=0
for i in range(N):
  a+=max([0,ls[i]-ls[i+1]])
print(a)

Submission status: Accepted

Solution code:
N=int(input())
H=list(map( int,input().split() ))
ans=0

while any(H) != 0:
  for i in range(N):
    if H[i]!=0:
      for j in range(i,N):
        if H[j]==0:
          break
        elif j==N-1:
          j+=1
      break
  for k in range(i,j):
    H[k]-=1
  ans+=1
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import numpy as np
N = int(input())
h = [int(x) for x in input().split()]
h = np.array(h,np.int64)
ans = 0
def split_(A):
    global ans
    if np.count_nonzero(A != 0) == 0:
        return
    else:
        minA = A.min()
        A -= minA
        ans += minA
        for i in range(len(A)):
            if A[i] == 0:
                split_(A[:i])
                split_(A[i+1:])
                break
split_(h)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int, input().split()))
# print(H)
count = 0
while sum(H) > 0:
    flag = 0
    for i, h in enumerate(H):
        if h > 0 and flag == 0:
            H[i] -= 1
            flag = 1
        elif h == 0 and flag == 1:
            break
        elif h == 0 and flag == 0:
            continue
        else:
            H[i] -= 1
    count += 1
    # print(H)
print(count)

Submission status: Accepted

Solution code:
n = int(input())
l = [int(i) for i in input().split()]

ans = 0
l.insert(0, 0)
for i in range(n):
    diff = l[i+1] - l[i] 
    if diff > 0:
        ans += diff
    else:
        pass

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
listh = list(map(int, input().split()))
listans = [listh[0]]
for n in range(1,N-1):
    if (listh[n+1]-listh[n])*(listh[n]-listh[n-1])<=0:
        listans.append(listh[n])
listans.append(listh[N-1])        
#print(listans)
l =len(listans)
listans2 = []
for n in range(l-1):
    if listans[n] == listans[n+1]:
        pass
    else:
        listans2.append(listans[n])
listans2.append(listans[l-1])        
#print(listans2)    
L = len(listans2)
#print(L)
listans3 = [listans2[0]]
for n in range(1,L-1):
    if (listans2[n+1]-listans2[n])*(listans2[n]-listans2[n-1])<=0:
        listans3.append(listans2[n])
listans3.append(listans2[L-1])        
#print(listans3)
ll =len(listans3)
listodd = [listans3[x] for x in range(ll) if x%2 ==1]
#print(listodd)
listeven = [listans3[x] for x in range(ll) if x%2 ==0]
#print(listeven)
if len(listodd) == 0:
    print(listeven[0])
elif ll%2 == 0:
    if listeven[0]>listodd[0]:
        del listodd[-1]
        ans = sum(listeven)-sum(listodd)
        print(ans)

    else:  
        del listeven[0]
        ans = sum(listodd)-sum(listeven)  
        print(ans)

else:     
    if listeven[0]>listodd[0]:
        ans = sum(listeven)-sum(listodd)
        print(ans)

    else:
        del listeven[-1]
        if len(listeven) >= 1:
            del listeven[0]
        ans = sum(listodd)-sum(listeven)
        print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline

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

def solve(l):
    ret = 0
    if len(l)==1:
        return l[0]
    elif 0 in l:
        part_l = []
        for i in l:
            if i != 0:
                part_l.append(i)
            else:
                if len(part_l)>0:
                    ret+=solve(part_l)
                    part_l = []
        if len(part_l)>0:
            ret+=solve(part_l)
        return ret
    else:
        min_l = min(l)
        ret+=min_l
        l = list(map(lambda x:x-min_l,l))
        return ret + solve(l)

print(solve(b))

Submission status: Accepted

Solution code:
n=int(input())
*L,=map(int,input().split())
w=0
b=0
for i in L:
      if i>b:
            w+=i-b
      b=i
print(w)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
ans = 0
m = 0
tmp_max = l[0]
check = False
for i in range(1, n):
    if l[i - 1] > l[i]:
        check = True
    elif check and l[i - 1] < l[i]:
        ans = ans + tmp_max - m
        tmp_max = l[i]
        m = l[i - 1]
        check = False
    elif not(check):
        tmp_max = l[i]
ans = ans + tmp_max - m
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline

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

def solve(l):
    ret = 0
    if len(l)==1:
        return l[0]
    elif 0 in l:
        part_l = []
        for i in l:
            if i != 0:
                part_l.append(i)
            else:
                if len(part_l)>0:
                    ret+=solve(part_l)
                    part_l = []
        if len(part_l)>0:
            ret+=solve(part_l)
        return ret
    else:
        min_l = min(l)
        ret+=min_l
        l = list(map(lambda x:x-min_l,l))
        return ret + solve(l)

print(solve(b))

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
h = np.array([int(i) for i in input().split()])
flag = [0] * N

thrs = 0

if N == 1:
  print(h[0])
  exit()

i = 0
ans = 0
l, r = (0, 0)
while sum(flag) != N:
  j = 0
  while j < l:
    if h[j] == 0:
      flag[j] = 1
    j += 1
  
  if h[l] == 0:
    l += 1
    flag[l] = 1
  
  i = 0
  i += l
  r = i
  while h[i] > thrs:
    r += 1
    i += 1
    if i == N:
      break
  
  if l == r:
    continue
  ans += min(h[l:r])
  h[l:r] = h[l:r] - min(h[l:r])
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
h=[0] + list(map(int,input().split()))

ans=0
for i in range(N):
  if h[i+1] > h[i]:
    ans+=h[i+1]-h[i]
print(ans)

Submission status: Accepted

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

cnt = 0

while sum(h_li) != 0:
    check = 0
    for i in range(n):
        
        if h_li[i] > 0:
            h_li[i] -= 1
            check = 1
    
        else:
            if check == 1:
                cnt += 1
                check = 0
    else:
        if check == 1:
            cnt += 1
            
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
ans=h[0]
for i in range(n-1):
    if h[i]<h[i+1]:
        ans+=h[i+1]-h[i]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())

h=[int(x) for x in input().split()]

height=[]

result=0
tmp=0

for i in range(max(h)):
    buf=[]
    for j in range(n):
        if h[j] > i:
            buf.append(1)
        else:
            buf.append(0)
    height.append(buf)

for val in height:
    while(tmp < len(val)):
        if val[tmp]==0:
            tmp+=1
        else:
            result+=1
            while(tmp<len(val)):
                if(val[tmp]==0):
                    break
                tmp+=1;
    tmp=0
#print(height)
print(result)

Submission status: Accepted

Solution code:
n = int(input())
h = [0] + [int(i) for i in input().split()]

ans = 0
for i in range(1, max(h) + 1):
    for j in range(n):
        if h[j] < i <= h[j + 1]:
            ans += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
hs = [int(val) for val in input().split(' ')]
# num = len(hs)
# start_idx = None
# end_idx = None
# op_num = 0
# last_op_idx = None
# while sum(hs) > 0:
#     for i in range(num):
#         if hs[i] > 0:
#             hs[i] -= 1
#             last_op_idx = i
#             if i == num - 1:
#                 op_num += 1
#         else:
#             if i >= 1 and last_op_idx == i-1:
#                 op_num += 1
#print(op_num)




def recursion(hs, op_num):
    if not hs:
        return op_num-1
    if min(hs) >= 1:
        return recursion([val - 1 for val in hs], op_num+1)

    results = []
    curr_results = []
    for i in range(len(hs)):
        if hs[i] >= 1:
            curr_results.append(hs[i]-1)
        else:
            results.append(curr_results)
            curr_results = []
    results.append(curr_results)
    for result in results:
        op_num += recursion(result, 1)
    return op_num

print(recursion(hs, 0))

Submission status: Accepted

Solution code:
N = int(input())
hs = [int(val) for val in input().split(' ')]
num = len(hs)
start_idx = None
end_idx = None
op_num = 0
last_op_idx = None
while sum(hs) > 0:
    for i in range(num):
        if hs[i] > 0:
            hs[i] -= 1
            last_op_idx = i
            if i == num - 1:
                op_num += 1
        else:
            if i >= 1 and last_op_idx == i-1:
                op_num += 1
print(op_num)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
f=[0]*n
cnt=0
while 1:
    flag=0
    for i in range(n):
        if h[i]!=f[i]:
            f[i]+=1
            flag=1
        elif h[i]==f[i] and flag==1:
            cnt+=1
            flag=0
    if flag==1:
        cnt+=1

    if h==f:
        break

print(cnt)

Submission status: Accepted

Solution code:
N = int(input().strip())
H = [int(str) for str in input().strip().split()]

ans = 0

for h in range(1, max(H) + 1):
    i = 0
    while i < N:
        if H[i] < h:
            i += 1
        else:
            ans += 1
            while i < N and H[i] >= h:
                i += 1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
h=[int(i) for i in input().split()]
#連続しているところがどこかを考えればいい
#O(n^2)

def not_all_0(a):
    for i in a:
        if i!=0:
            return True
    else:
        return False

c=0
while(not_all_0(h)):
    d=0
    for i in range(n):
        if d==0:
            if h[i]!=0:
                h[i]-=1
                c+=1
                d=1
        else:
            if h[i]!=0:
                h[i]-=1
            else:
                d=0
print(c)

Submission status: Accepted

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

s = [0] * n
cnt = 0

while True:
    flag = 0
    for i in range(n):
        if h[i] != s[i]:
            s[i] += 1
            flag = 1
        elif h[i] == s[i] and flag:
            cnt += 1
            flag = 0
    if flag:
        cnt += 1

    if h == s:
        break

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
ans = h[0]
for i in range(N-1):
  if h[i+1] > h[i]:
    ans += h[i+1]-h[i]
print(ans)

Submission status: Accepted

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

ans, base, h_last = 0, 0, 0
for h in H:
    if h < h_last:
        ans += h_last - base
        base = h
    h_last = h
print(ans + H[-1] - base)

Submission status: Accepted

Solution code:
N=int(input())
H=[0]+[int(x) for x in input().split()]
P=max(H)
count=0
for i in range(P):
  Q=P-i
  for j in range(1,N+1):
    if H[j-1]<Q and H[j]>=Q:
      count +=1
print(count)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
cnt=0
ch=True
#print(cnt,a)
while True:
    my_bool=True
    for i in range(n):
        if my_bool:
            if a[i]!=0:
                my_bool=False
                ch2=True
                a[i]-=1
        else:
            if a[i]!=0:
                a[i]-=1
                ch=True
                ch2=True
            else:
                if ch2:
                    cnt+=1
                ch=False
                ch2=False
    if ch:
        if my_bool:
            pass
        else:
            #print("ch",cnt,a)
            cnt+=1
    #print(cnt,a)
    if my_bool:
        break
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
ans=0
active=0
for i in range(n):
  if active >= h[i]:
    active = h[i]
  else:
    ans += h[i]-active
    active = h[i]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
x=[0]*n
cnt=0
while a!=x:
    flag=True
    for i in range(n):
        if x[i]!=a[i]:
            x[i]+=1
            if i==n-1:
              cnt+=1
        else:
            continue
        for j in range(i+1,n):
            if x[j]!=a[j]:
                x[j]+=1
                if j==n-1:
                    cnt+=1
                    flag=False
                
            else:
                cnt+=1
                
                flag=False
                break
        if flag==False:
          break
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
array = input().split(' ')
harray = []
for z in array:
    z = int(z)
    harray.append(z)
counter = 0
while harray != [0]*N:
    if 0 not in harray:
        a = min(harray)
        for i in range(len(harray)):
            harray[i] -= a
        counter += a
    else:
        zeroarray = [j for j, x in enumerate(harray) if x == 0]
        if len(zeroarray) == 1:
            if zeroarray[0] == 0:
                a = min(harray[1:])
                if len(harray) == 2:
                    harray[1] -= a
                else:
                    for y in range(1,N):
                        harray[y] -= a
                counter += a
            elif zeroarray[0] == N-1:
                a = min(harray[:N-1])
                for y in range(N-1):
                    harray[y] -= a
                counter += a
            else:
                a = min(harray[:zeroarray[0]])
                for y in range(zeroarray[0]):
                    harray[y] -= a
                counter += a
                a = min(harray[zeroarray[0]+1:])
                if zeroarray[0]+1 != N:
                    for y in range(zeroarray[0]+1,N):
                            harray[y] -= a
                else:
                    harray[N-1] -= a
                counter += a
        else:
            if zeroarray[0] != 0:
                    a = min(harray[:zeroarray[0]])
                    for y in range(zeroarray[0]):
                        harray[y] -= a
                    counter += a
            if zeroarray[-1] != N-1:
                    a = min(harray[zeroarray[-1]+1:])
                    if zeroarray[-1]+1 != N:
                        for y in range(zeroarray[-1]+1,N):
                            harray[y] -= a
                    else:
                        harray[N-1] -= a
                    counter += a
            for k in range(len(zeroarray)-1):
                
                if zeroarray[k+1] - zeroarray[k] == 1:
                    continue
                else:
                    a = min(harray[zeroarray[k]+1:zeroarray[k+1]])
                    for y in range(zeroarray[k]+1,zeroarray[k+1]):
                        harray[y] -= a
                    counter += a
                

print(counter)

Submission status: Accepted

Solution code:
N = int(input())
H = [list(map(int, input().split()))]

ans = 0
while H:
    h = H.pop(0)
    if not h: continue

    min_h = min(h)
    ans += min_h
    
    s = 0
    
    for i in range(len(h)):
        h[i] -= min_h
        
        if h[i]: continue
        H.append(h[s:i])
        s = i+1
    if s != N: H.append(h[s:i+1])

print(ans)

Submission status: Accepted

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

hmax = max(H)
ans = 0

def calc(H):
  n = 0
  count = 0
  for i in H:
    if i == 0:
      if count != 0:
        n += 1
      count = 0
    else:
      count += 1
  return n

for i in range(hmax):
  H.append(0)
  ans += calc(H)
  del H[-1]
  H = list(map(lambda x: max(0, x - 1), H))

print(ans)

Submission status: Accepted

Solution code:
def count(A, a):
    count = 1 if A[0] >= a else 0
    for i in range(len(A)-1):
        if A[i] < a and A[i+1] >= a:
            count += 1
    return count

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

ans = 0
pre_h = 0
for h in sorted(set(H)):
    ans += count(H, h) * (h - pre_h)
    pre_h = h
print(ans)

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    H = list(map(int, input().split()))
    ret = 0
    flag = True
    
    while flag:
        flag = False
        continuous = False
        num_countinuous = 0
        # print(H)
        for i in range(N):
            if H[i] > 0:
                H[i] -= 1
                flag = True
                if not continuous:
                    num_countinuous += 1
                    continuous = True
            else:
                continuous = False
                
        ret += num_countinuous
        
    print(ret)
    
solve()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
h = list(map(int, input().split()))
res = 0
mh = max(h)
for i in range(mh * N + 1):
  x = N
  for j in range(N - 1):
    if h[j] > 0 and h[j + 1] > 0:
      x -= 1
  for j in range(N):
    if h[j] == 0:
      x -= 1
      continue
    h[j] -= 1
  #print(h)
  res += x
  if x == 0: break

print(res)

Submission status: Accepted

Solution code:
import sys
import copy
N=int(input())
H = [int(x) for x in input().split()]

if N==1:
    print(H[0])
    sys.exit()

cnt=0
while True:
    f=True
    for h in H:
        if h!=0:
            f=False
            break
    if f:
        break

    H_new = copy.copy(H)
    if H[0]!=0:
        if H[0]!=0:
            cnt+=1
        H_new[0]-=1
    for i in range(1,len(H)):
        if H[i-1]==0 and H[i]!=0:
            cnt+=1
        if H_new[i]!=0:
            H_new[i]-=1
    H = copy.copy(H_new)


print(cnt)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

N = int(input().strip())
H = [int(x) for x in input().strip().split()]

G = [0] * N
ans = 0
while H != G:
    ans += 1
    f = False
    for n in range(N):
        if f:
            if G[n] == H[n]:
                break
            else:
                G[n] += 1
        else:
            if G[n] != H[n]:
                G[n] += 1
                f = True
            else:
                continue

print(ans)

Submission status: Accepted

Solution code:

import sys

input = sys.stdin.readline

# input = sys.stdin.readline
sys.setrecursionlimit(2147483647)


def split(h):
    pieces = []
    tmp = []
    for i in range(len(h)):
        if h[i] == 0:
            pieces.append(tmp)
            tmp = []
        else:
            tmp.append(h[i])

    pieces.append(tmp)
    return pieces


def f(h):
    if len(h) == 0:
        return 0

    ith = 0
    while True:
        maxh = max(h)
        if maxh == 0:
            break
        for i in range(len(h)):
            h[i] += 1
        ith += 1

    pieces = split(h)

    m = 0
    for piece in pieces:
        m += f(piece)

    return ith + m


def main():
    N = int(input())
    h = list(map(int, input().split()))
    h = [-i for i in h]

    pieces = split(h)

    m = 0
    for piece in pieces:
        m += f(piece)

    print(m)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
li = [0]+li+[0]

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

L = [x for x in L if x>0]
print(sum(L))

Submission status: Accepted

Solution code:
n=int(input())
h=[int(i) for i in input().split()]
j=0
ans=0
while j<max(h):
  if h[0]>j:
    ans+=1
  for i in range(1,n):
    if h[i-1]<=j and h [i]>j:
      ans+=1
  j+=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
k = 0
for  i in range(N-1):
   k += min(h[i], h[i+1])
print(sum(h) - k)

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
h = list(map(int, input().split()))
count = 0
water=deque()
while sum(h)!=0:
    for i in range(N):
        if h[i] != 0:
            water.append(i)
        if water and h[i]==0:
            break
    for i in range(len(water)):
        k=water.popleft()
        h[k]-=1
    count += 1
    water.clear()

print(count)

Submission status: Accepted

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

cnt = 0
for i in range(1, 100+1):
  prev = False
  for x in h:
    if x >= i and not prev:
      cnt += 1
    prev = x >= i 

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
h = [0]+[int(i) for i in input().split()]+[0]

ans = 0

for i,j in zip(h,h[1:]):
  ans += max(0,j-i)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
li=list(map(int,input().split()))
ans=0
for i in range(n-1):
  ans+=max((li[i]-li[i+1]),0)
ans+=li[n-1]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
ans = 0 #必要な最小の水やり操作の回数
active = 0 #直前の花の高さ

for i in range(n):
    if active < h[i]: #直前の花より大きかったら(h[i]-前の花の高さ)を求める
        ans += h[i] - active
    active = h[i]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
list1 = list(map(int,input().split()))
count = 0
def mizuyari(a,b):
	global count
	global list1
	for i in range(a,b):
		list1[i] -= 1
	count += 1

def search():
	global list1
	flag = 0
	for i in range(n):
		if list1[i] != 0:
			s = i
			break
	for j in range(s+1,len(list1)):
		if list1[j] == 0:
			mizuyari(s,j)
			flag = 1
			break
	if not flag:
		mizuyari(s,len(list1))

if __name__ == "__main__":
	while sum(list1) != 0:
		search()
	print(count)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
h.append(0)

res = 0
while True:
    if max(h) == 0:
        break
    for i in range(N):
        if h[i] > 0:
            l = i
            break
    for j in range(i+1, N+1):
        if h[j] == 0:
            r = j
            break
    
    x = min(h[l:r])
    for i in range(l, r):
        h[i] -= x
    res += x

print(res)

Submission status: Accepted

Solution code:

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

def find_res(h):
    count = 0

    if h == []:
        return 0
    if len(h) == 1:
        return h[0]

    while len(h) > 1:
        if 0 in h:
            idx = h.index(0)
            # print(h[0:idx], h[idx+1:])
            count +=  find_res(h[0:idx]) + find_res(h[idx+1:])
            return count
        else:
            min_h = min(h)
            h = [a - min_h for a in h]
            count += min_h

    return count

ans = find_res(h)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int, input().split()))
ans = 0
for i in range(N):
    if i == N-1:
        ans += H[i]
    elif H[i] > H[i+1]:
        ans += H[i] - H[i+1]
print(ans)

Submission status: Accepted

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

cnt = height[0]
for i in range(n-1):
    cnt += max(0, height[i+1]-height[i])

print(cnt)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(20000)

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

counter = 0


def devzero(l,moji=0):
    return_L,tmp = [],[]
    for val in l:
        if val != moji:
            tmp.append(val)
            #print(tmp)
        elif val == moji:
            return_L.append(tmp)
            tmp = []
    return_L.append(tmp)

    return return_L

def rec(L):
    global counter

    if L == []:
        return
    if 0 not in L:
        L = [i-1 for i in L]
        counter += 1
    L = devzero(L,moji=0)
    for val in L:
        rec(val)

if __name__=="__main__":
    rec(h_L)
    print(counter)

Submission status: Accepted

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

def splitList(ks):
  temp = []
  for k in ks:
    if k == 0:
      if temp:
        yield temp
        temp = []
    else:
      temp.append(k)
  if temp:
    yield temp

def f(ks):
  if not ks:
    return 0
  n = min(ks)
  ks_ = [k-n for k in ks]
  kss = list(splitList(ks_))
  return n + sum(map(f, kss))

print(f(ks))

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
cnt = 0
v = 0
for i in h:
    if i > v:
        cnt += i - v
    v = i
print(cnt)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    H=[0]+list(map(int,input().split()))
    ans=0
    for i in range(n):
        ans+=max(0,H[i+1]-H[i])
    print(ans)
resolve()

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.append(0)
ans = 0
for i in range(max(l)):
    for j in range(n):
        if l[j] != 0:
            l[j] -= 1
            if l[j + 1] == 0:
                ans += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int, input().split()))
H.insert(0, 0)

ans = 0
while max(H) > 0:
    group = 0
    for i in range(1, N+1):
        if H[i-1] == 0 and H[i] > 0:
            group += 1
    ans += group
    for i in range(1, N+1):
        if H[i] > 0:
            H[i] -= 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split())) + [0]
ans = 0
#  最大h×N本の全探索
#  連続が途切れる数を下からカウントすれば答えとなる
for i in range(max(h)):
    for j in range(N):
        if h[j] - i > 0 and h[j + 1] - i <= 0:
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
hi = list(map(int, input().split()))
flag = False
cnt = 0
while sum(hi)>0:
    flag = False
    for hidx in range(len(hi)):
        if hi[hidx] > 0:
            if not flag:
                cnt += 1
                flag = True
            hi[hidx] -= 1
        else:
            flag = False
print(cnt)