首页 > 代码库 > Codeforces Round #265 (Div. 2) B. Inbox (100500)

Codeforces Round #265 (Div. 2) B. Inbox (100500)

题目链接:http://codeforces.com/problemset/problem/465/B


B. Inbox (100500)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Over time, Alexey‘s mail box got littered with too many 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 integer n (1?≤?n?≤?1000) — the number of letters in the mailbox.

The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-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
5
0 1 0 1 0
output
3
input
5
1 1 0 0 1
output
4
input
2
0 0
output
0
Note

In the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.

In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.

In the third sample all letters are already read.



题意:

Alexey要查看邮件,邮件有很多并且已经有一定的顺序,顺序不能调换,问最少要进行多少次点开和关闭的操作才能查看完所有的邮件;


思路:

直接模拟一下就好了!只要下一邮件已经阅读过了就直接关闭当前邮件,反之直接阅读下一邮件! 注意最后一封邮件的状态!


代码如下:

#include <cstdio>

int main()
{
    int n;
    int a[1017];
    while(scanf("%d",&n)!=EOF)
    {

        for(int i = 1; i <= n; i++)
        {
            scanf("%d",&a[i]);
        }
        int k = 0;
        for(int i = 1; i < n; i++)
        {
            if(a[i]==1&&a[i+1]==1)
                k++;
            else if(a[i]==1&&a[i+1]==0)
                k+=2;
        }
        if(!a[n]&&k>0)
            k--;
        else if(a[n])
            k++;
        printf("%d\n",k);
    }
    return 0;
}



Codeforces Round #265 (Div. 2) B. Inbox (100500)