To offer you the better approach. Can you explain me the logic of your code? Because right now it's just changing the loop variable which leads the exit from loop.
def calc(a):
n = len(a)
p2 = su = 0
for i in range(n):
if a[i] != 0:
b, m2 = False, 0
for j in range(i+1, n):
if a[i] < a[j]:
for k in range(i+1, j):
su += a[i] - a[k]
i = j-1
b = False
break
if m2 < a[j]:
m2 = a[j]
b = True
print(i, j, su)
if b:
p1 = i
for k in range(i+1, p1):
su += a[p1] - a[k]
print(su)