Codeforces round #265 (Div. 2) B. inbox (100500)

Source: Internet
Author: User

Over time, Alexey's mail box got littered with too character letters. Some of them are read, while others are unread.

Alexey's mail program can either show a list of all letters or show the content of a single letter. as soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens ). in one click he can do any of the following operations:

  • Move from the list of letters to the content of any single letter.
  • Return to the list of letters from single letter viewing mode.
  • In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.

The program cannot delete the letters from the list or rearrange them.

Alexey wants to read all the unread letters and go watch football. now he is viewing the list of all letters and for each letter he can see if it is read or unread. what minimum number of operations does Alexey need to perform to read all unread letters?

Input

The first line contains a single integerN(1? ≤?N? ≤? 1000)-the number of letters in the mailbox.

The second line containsNSpace-separated integers (zeros and ones)-the State of the letter list.I-Th number equals either 1, ifI-Th number is unread, or0, ifI-Th letter is read.

Output

Print a single number-the minimum number of operations needed to make all the letters read.

Sample test (s) Input
50 1 0 1 0
Output
3
Input
51 1 0 0 1
Output
4
Input
20 0
Output
0. Question: view the email and find the number of operations required. Thought: Question, simulate
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int main() {int n, num[1010];scanf("%d", &n);for (int i = 1; i <= n; i++)scanf("%d", &num[i]);int ans = 0;for (int i = 0; i < n; i++)if (num[i] && num[i+1])ans++;else if (num[i] && !num[i+1])ans += 2;if (!num[n] && ans)ans--;else if (num[n])ans++;printf("%d\n", ans);return 0;}



Codeforces round #265 (Div. 2) B. inbox (100500)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.