CF465B.Inbox (100500)

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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?

随着时间推移,Alexey 的邮箱中积压了过多的邮件。其中一些已被阅读,另一些则尚未阅读。

Alexey 的邮件程序有两种视图模式:一种是显示所有邮件的列表,另一种是显示某一封邮件的正文内容。一旦程序显示了一封未读邮件的正文内容,该邮件即自动标记为已读(若显示的是已读邮件的正文,则无任何变化)。每次单击,Alexey 可以执行以下任一操作:

  • 从邮件列表视图切换到任意一封邮件的正文视图;
  • 从单封邮件的正文视图返回邮件列表视图;
  • 在单封邮件的正文视图下,切换到列表中的上一封或下一封邮件(注意:不能从第一封邮件向前切换,也不能从最后一封邮件向后切换)。

该程序不允许从列表中删除邮件,也不允许重新排列邮件顺序。

Alexey 希望阅读所有未读邮件,然后去看足球赛。目前他正处在邮件列表视图下,并且对每封邮件是否已读都一目了然。那么,Alexey 至少需要执行多少次操作,才能将所有未读邮件全部阅读完毕?

输入格式

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.

第一行包含一个整数 nn(1≤n≤10001 \leq n \leq 1000)—— 表示邮箱中信件的数量。

第二行包含 nn 个用空格分隔的整数(仅含 0 和 1)—— 表示信件列表的状态。其中第 ii 个数字为 1 表示第 ii 封信件未读,为 0 表示第 ii 封信件已读。

输出格式

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

输出一个整数——使所有字母均可读所需的最少操作次数。

输入输出样例

  • 输入#1

    5
    0 1 0 1 0

    输出#1

    3
  • 输入#2

    5
    1 1 0 0 1

    输出#2

    4
  • 输入#3

    2
    0 0

    输出#3

    0

说明/提示

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 需要执行三次操作才能完成任务:打开第二个字母,移动到第三个字母,再移动到第四个字母。

在第二个样例中,操作方案为:打开第一个字母,移动到第二个字母,返回列表,再打开第五个字母。

在第三个样例中,所有字母都已被阅读。

输入解题思路,AI测评打分。不知道怎么写?

首页