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

for i in reversed(range(1, n)):
	for j in range(i):
		a[j] = min(a[j], a[i] - 1 if a[i] > 0 else 0)

print(sum(a))<commit_msg>Fix: solve in O(n) using watermark<commit_after>n = int(input())
a = list(map(int, input().split()))

watermark = a[-1]
for i in reversed(range(n - 1)):
	if a[i] < watermark:
		watermark = a[i]
	else:
		a[i] = max(watermark - 1, 0)
		watermark = a[i]

print(sum(a))
{'fraction_non_alphanumeric': 0.21739130434782608, 'fraction_numerical': 0.017391304347826087, 'mean_word_length': 4.238636363636363, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4409055', 'n_tokens_mistral': 214, 'n_tokens_neox': 205, 'n_words': 53}