codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

465B.md (2173B)


      1 Source: https://codeforces.com/problemset/problem/465/B
      2 
      3 B. Inbox (100500)
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 stdin
     16 
     17 output
     18 
     19 stdout
     20 
     21 Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.
     22 
     23 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:
     24 
     25 * Move from the list of letters to the content of any single letter.
     26 * Return to the list of letters from single letter viewing mode.
     27 * 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.
     28 
     29 The program cannot delete the letters from the list or rearrange them.
     30 
     31 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?
     32 
     33 Input
     34 
     35 The first line contains a single integer *n* (1 ≤ *n* ≤ 1000) — the number of letters in the mailbox.
     36 
     37 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.
     38 
     39 Output
     40 
     41 Print a single number — the minimum number of operations needed to make all the letters read.
     42 
     43 Examples
     44 
     45 Input
     46 
     47 ```
     48 5  
     49 0 1 0 1 0
     50 ```
     51 
     52 Output
     53 
     54 ```
     55 3
     56 ```
     57 
     58 Input
     59 
     60 ```
     61 5  
     62 1 1 0 0 1
     63 ```
     64 
     65 Output
     66 
     67 ```
     68 4
     69 ```
     70 
     71 Input
     72 
     73 ```
     74 2  
     75 0 0
     76 ```
     77 
     78 Output
     79 
     80 ```
     81 0
     82 ```
     83 
     84 Note
     85 
     86 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.
     87 
     88 In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.
     89 
     90 In the third sample all letters are already read.