Counting Score

Problem

An OX problem is a problem that has two possible answers: right or wrong. In the case of consecutive correct answers in a test made of multiple OX questions, it was decided to calculate the score as follows to give additional points. If the first question is correct, it is counted as 1 point. Incorrect answers to the previous questions are counted as 1 point for the first correct answer. In addition, if the answers to the questions are correct in succession, the second question is counted as 2 points, the third question as 3 points, …, and the Kth question is counted as K points. Incorrect questions are counted as 0 points.
For example, in the 10 OX questions below, when the correct answer is indicated as 1 and incorrect answers are indicated as 0, the score is calculated as shown in the table below, and the total score is 1+1+ 2+3+1+2=10 points.

1011100110
answer 1 0 1 1 1 0 0 1 1 0
score 1 0 1 2 3 0 0 1 2 0

Write a program that calculates the total score given the scoring results of a test question.

input

The first line gives the number of problems N (1 ≤ N ≤ 100). In the second line, 0 or 1 indicating the scoring result of N questions is given with a blank space in between. 0 means that the answer to the question is incorrect, and 1 means that the answer to the question is correct.

10
1011100110

output

In the first line, the total score considering the added points for the scoring results given in the input is output.

10

Solution

function solution(arr) {
	let answer = 0,
		cnt = 0;

	if (arr[0] === 1) answer++;

	for (let i = 1; i < arr.length; i++) {
		if (arr[i] === 1) {
			cnt++;
			answer += cnt;
		} else cnt = 0;
	}

	return answer;
}