Skip to content

Instantly share code, notes, and snippets.

@HauptJ
Last active February 15, 2020 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HauptJ/841e7a2c940955e567566371edb49d45 to your computer and use it in GitHub Desktop.
Save HauptJ/841e7a2c940955e567566371edb49d45 to your computer and use it in GitHub Desktop.
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the candies function below.
def candies(n, arr):
n = len(arr)
dp = [1]*n
sum = 0
for i in range(1, n, 1):
if arr[i] > arr[i-1]:
dp[i] = dp[i] + dp[i-1]
for i in range(n-2, -1, -1):
if arr[i] > arr[i+1] and dp[i] <= dp[i+1]:
dp[i] = dp[i+1] + 1
for d in dp:
sum += d
return sum
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input())
arr = []
for _ in range(n):
arr_item = int(input())
arr.append(arr_item)
result = candies(n, arr)
fptr.write(str(result) + '\n')
fptr.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment