Alena has successfully passed the entrance exams to the university and is now looking forward to start studying.
One two-hour lesson at the Russian university is traditionally called a pair, it lasts for two academic hours (an academic hour is equal to 45 minutes).
The University works in such a way that every day it holds exactly n lessons. Depending on the schedule of a particular group of students, on a given day, some pairs may actually contain classes, but some may be empty (such pairs are called breaks).
The official website of the university has already published the schedule for tomorrow for Alena's group. Thus, for each of the n pairs she knows if there will be a class at that time or not.
Alena's House is far from the university, so if there are breaks, she doesn't always go home. Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university.
Of course, Alena does not want to be sleepy during pairs, so she will sleep as long as possible, and will only come to the first pair that is presented in her schedule. Similarly, if there are no more pairs, then Alena immediately goes home.
Alena appreciates the time spent at home, so she always goes home when it is possible, and returns to the university only at the beginning of the next pair. Help Alena determine for how many pairs she will stay at the university. Note that during some pairs Alena may be at the university waiting for the upcoming pair.
Input
The first line of the input contains a positive integer n (1 ≤ n ≤ 100) — the number of lessons at the university.
The second line contains n numbers ai (0 ≤ ai ≤ 1). Number ai equals 0, if Alena doesn't have the i-th pairs, otherwise it is equal to 1. Numbers a1, a2, ..., an are separated by spaces.
Output
Print a single number — the number of pairs during which Alena stays at the university.
Examples
Input
5
0 1 0 1 1
Output
4
Input
7
1 0 1 0 0 1 0
Output
4
Input
1
0
Output
0
Note
In the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be it the university waiting for the next pair.
In the last sample Alena doesn't have a single pair, so she spends all the time at home.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given n (1 ≤ n ≤ 100) representing the number of two-hour lessons (pairs) in a day. Each pair can be a class (1) or break (0). Alena will only come to the first pair that is a class (1). She will not come if there are no classes? Actually, the problem says: "she will sleep as long as possible, and will only come to the first pair that is presented in her schedule." That means she will come to the first class (the earliest i such that a_i = 1). If there is no class at all, she stays home; output 0.
She also leaves after the last class? "Similarly, if there are no more pairs, then Alena immediately goes home." So after the last pair (i=n), if she is at the university, she will go home immediately? Actually, the wording: "if there are no more pairs, then Alena immediately goes home." That means when the schedule ends (after the n-th pair), she goes home. But we count pairs during which she stays at the university. So we need to count the number of pairs (i from 1 to n) that she is present at the university (either attending a class or waiting for next class during a break, but only if she is at the university during that break). She goes home during breaks that consist of at least two free pairs in a row. If a break is only a single free pair between classes, she stays at the university waiting. Also, at the beginning, she only comes to first class, not earlier. So any breaks before the first class, she is not at university (she is at home sleeping). At the end, after the last class, she might leave immediately? The phrase: "if there are no more pairs, then Alena immediately goes home." That suggests after the last class, if there are no more pairs (i.e., after the last pair, the day ends), she goes home. But does she count as present at the university during the break after the last class if it exists? Let's read carefully.
We have a sequence of length n. Some positions are classes (1) and some breaks (0). Alena's behavior:
She starts at home.
She will only come to the first pair that is a class. So she skips all initial breaks (0s) at the beginning.
Once she arrives at the university for a class, she attends that class. She then decides after each class whether to go home if there is a break of at least two consecutive zeros upcoming, or stay if not.
She "always goes home when it is possible, and returns to the university only at the beginning of the next pair."
Also, "if there are no more pairs, then Alena immediately goes home." That means when the schedule ends (i.e., after the last pair of the day), she goes home. But also, presumably after the last class, if there are trailing breaks, she might leave? Actually, "if there are no more pairs" might refer to the situation when she is at a break and there is no upcoming pair? Or it's general: after the last pair, she goes home. But does that include the case where the last pair is a break and she is present? Let's parse.
Better to reconstruct the process step by step.
We have a timeline: pairs 1 to n. At each pair, Alena can be either at home or at university. At the start of pair 1, she is at home. She looks at the schedule? She will come only to the first class. That means she checks pairs sequentially: if the pair is a break (0), she stays home (sleeps). As soon as she sees a class (1) at time i, she will go to the university and be present for that pair (the class). From then on, she is engaged.
Now, after attending a class at pair i, what does she do? She looks ahead to the upcoming pairs. If there is a break of at least two consecutive zeros (i.e., positions i+1 and i+2 both 0, and presumably i+1 is within n), then she can go home during that break. But the rule: "Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university." So the decision to go home is based on the immediate upcoming break (the consecutive zeros after the current class). But careful: It might be that after a class, there is a break that is either a single zero followed by a class, or a double zero (or more). If there are at least two zeros, she can go home and come back at the beginning of the next pair after the break? Actually: "returns to the university only at the beginning of the next pair." So if she goes home during a break, she will come back at the start of the pair that follows the break (i.e., the first class after the break). But the break may consist of more than two zeros; she goes home and stays home during all of those break pairs? The problem says "Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university." This suggests that if there is a break of length >=2, she goes home for the duration of the break (maybe she leaves immediately after the class and comes back just before the next class after the break). But does she count as present during those break pairs? She is at home, so not at university. So we shouldn't count those pairs.
If the break is exactly one zero (i.e., a single free pair between classes), she does not go home; she waits at the university during that zero pair, so she is present for that zero pair.
Also, after the last class, there might be trailing zeros. The rule: "if there are no more pairs, then Alena immediately goes home." That likely means after the last pair of the day (i=n), she goes home regardless. But if the last class is before n, and there are trailing zeros, what does she do? She would have just attended the class. Then she looks ahead: there is a break (zero or more zeros) until the end. If the break length is >=2? Actually, the condition for going home is that the break consists of at least two free pairs in a row. If after the last class, the remaining pairs are all zeros. Let's denote the index of the last class is L. For i = L, after the class, she will check the upcoming sequence. If the remaining zeros count is at least 2, she would go home during that break. But then the day ends after n. Since she is at home, she stays home for those zeros. That seems plausible. However, there is also a statement: "Similarly, if there are no more pairs, then Alena immediately goes home." That might be a separate rule: if there are no more pairs (i.e., after the last class, there are no pairs at all, meaning the class was at n? Actually, "no more pairs" could mean that the schedule has ended; so after pair n, she goes home. But the more precise interpretation: When she is at the university after a class, and there is no upcoming pair (i.e., the class was at the last pair, i=n), then she goes home immediately (so she does not stay for any non-existent pair). But if there are trailing zeros, those are pairs, so there are more pairs. So the statement "if there are no more pairs, then Alena immediately goes home" is relevant only when she is at a class that is the last pair? Or after she finishes a class at position i, she might consider going home; but if i = n, there are no more pairs, so she goes home right away (and we count that she attended the class at pair n, but not any after). This matches the description: after the last class, she goes home. So for trailing zeros, if they exist after the last class, they are pairs. She will decide after the last class whether to go home or not. Since the break after last class is of length (n - L). If that length >= 2, she goes home; if length = 1, she might stay? But the rule says: she goes home if the break consists of at least two free pairs in a row. If it's exactly one free pair after the last class, does she wait at the university for that one pair? But then after that pair, there are no more pairs, so she would go home immediately after that pair? The statement: "Similarly, if there are no more pairs, then Alena immediately goes home." So if after a class there is a single free pair, she will wait at the university during that free pair (since it's not >=2). Then after that pair, there are no more pairs (since that was the last), she goes home. So she would be present for that zero pair. That seems consistent: she is at university during that trailing single zero. However, is that realistic? She waits at the university for a break that is only one pair, but then the day ends and she goes home. So she is present during that zero pair.
But note: In the first sample, schedule: 0 1 0 1 1. Output = 4. Let's analyze:
Pairs: 1:0, 2:1, 3:0, 4:1, 5:1.
Alena comes to first class at pair 2. So she is not present at pair 1.
Now after pair 2 (class), what does she do? Look at upcoming: pair 3 is 0, pair 4 is 1. The break length = 1 (only one zero). Since it's not at least 2, she stays at university during pair 3. So she is present at pair 3.
Then at pair 4 (class), she attends. After pair 4, upcoming: pair 5 is 1 (class). There is no break of zeros. Actually after class 4, next pair is class. So there is no break at all? The break length of consecutive zeros is 0 (since next is 1). She does not go home because there is no break? The rule is about breaks: if there is a break (some zeros) and its length >=2, she goes home. But if the next pair is a class, then she doesn't have any break to go home. She stays at university and attends the next class. So after pair 4, she will be present at pair 5. Then after pair 5 (class), there are no more pairs (n=5). So she goes home immediately. So total present pairs: 2,3,4,5 => 4. So matches output.
Now second sample: n=7, a = 1 0 1 0 0 1 0. Output = 4.
Let's simulate.
Pairs: 1:1, 2:0, 3:1, 4:0, 5:0, 6:1, 7:0.
First class at pair 1 (since it's the first pair and it's a class). She arrives for pair 1. Present at pair1.
After pair1: upcoming: pair2=0, pair3=1. Break length=1 (only one zero). So she stays at university during pair2. Present at pair2.
At pair3: it's a class; she attends. Present at pair3.
After pair3: upcoming: pair4=0, pair5=0, pair6=1. Break length = 2 (pair4 and pair5 are zeros). Since >=2, she can go home. So she goes home after pair3. She will return to university at the beginning of the next pair after the break? The rule: "returns to the university only at the beginning of the next pair." So after a break, she returns at the beginning of the first pair that is not part of the break? Actually, "next pair" likely means the pair that follows the break, i.e., the class at pair6. She will not be present during pairs 4 and 5 (since she is at home). So not present at 4,5.
Then at pair6: she returns? She arrives at the beginning of pair6 (the class). Present at pair6.
After pair6: upcoming: pair7=0, then end. Break length = 1 (single zero). Since only one zero, she stays at university during pair7. Present at pair7.
After pair7, no more pairs, she goes home.
Total present: pairs 1,2,3,6,7 = 5? That's 5, but output is 4. So I must have miscount. Let's list: 1,2,3,6,7 = 5. But expected output is 4. So something is off.
Maybe she does not come to pair1? Wait, the first class is at pair1. The problem says: "she will sleep as long as possible, and will only come to the first pair that is presented in her schedule." That implies she comes to the first pair that is a class. So if pair1 is a class, she should come. So why output is 4? Let's double-check sample input:
7
1 0 1 0 0 1 0
Output: 4
Maybe my interpretation about when she goes home after a class is not exactly about the immediate upcoming break length. Let's re-read the problem statement precisely:
"Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university.
Of course, Alena does not want to be sleepy during pairs, so she will sleep as long as possible, and will only come to the first pair that is presented in her schedule. Similarly, if there are no more pairs, then Alena immediately goes home.
Alena appreciates the time spent at home, so she always goes home when it is possible, and returns to the university only at the beginning of the next pair."
Now the critical part: "Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university." This could be interpreted that when there is a break (a sequence of zeros) she can go home during that break only if its length is at least 2; else she stays. But when does she decide to go home? After a class? Or when she is at the university and there is an upcoming break? Let's think of her schedule as a sequence of events.
Another possible interpretation: At any time when she is at the university, if there is an upcoming break (a consecutive block of zeros) that is long enough (≥2), she will go home for the duration of that break. However, if she is already at home (e.g., after going home), she will only return at the beginning of the next pair (i.e., the class after the break). But also, if she is at home, she does not come back earlier.
Now the confusion: In the second sample, maybe she does not attend pair1 because she sleeps as long as possible and only comes to the first pair that is presented? But pair1 is presented; that is the first pair with a class. But perhaps "presented" means that she has a class? Usually yes. So she should come.
Alternatively, maybe she counts the number of pairs she stays at the university, but the answer counts only the pairs where she is physically at the university, and maybe if she arrives exactly at the start of a class, does she count as being at the university for that entire pair? Yes, as per first sample, she counted pair2 (the first class) as present. So pair1 should be counted if she attends.
But then why does my simulation yield 5 and answer is 4? Let's check if perhaps she goes home after pair1? After pair1, break length = 1 (pair2 is zero, then pair3 is class). Since break length is 1, she does NOT go home; she stays at university during pair2. That gives present at pair2. That seems correct. Then after pair3, break length = 2, she goes home, so she is not present for 4,5. Then she comes back for pair6 and then stays for pair7. So that's 5. So maybe after pair6, there is a trailing zero, break length = 1, so she stays? That gives present at 7. So still 5.
Perhaps she does not come to pair7 because after pair6, she might go home? Wait, after pair6, there is pair7 which is 0. But if she is at university after pair6 and there is a break of length 1, she waits at university for that break. So she should be present at pair7. So why answer 4? Something else: maybe the rule "if there are no more pairs, then Alena immediately goes home." Could be applied after a break? Actually, maybe after she goes home during a long break, she returns at the beginning of the next pair, which is pair6. Then after pair6, she sees the next pair is 0, but since the day ends after pair7? Actually, there is a pair7. So there is a pair7. So she would not go home immediately after pair6 because there is a pair7. But maybe she goes home after pair6 because the break is only 1 and she can go home? No, rule says she can go home only if break >=2. So she stays.
Maybe the answer counts only pairs where she is physically in a class (attending), not waiting? But the first sample counts pair3 which is a break (0) as present because she was waiting. So they count waiting as well.
Maybe I've misread the input: The second sample: "1 0 1 0 0 1 0". Let's count output 4. Could it be that she does not come to pair1 because she sleeps as long as possible? But then the first class would be pair3? Actually, the first 1 is at pair1. But maybe she only comes to the first pair that is "presented" meaning the first pair of the day? No, that doesn't make sense.
Let's re-express: "she will sleep as long as possible, and will only come to the first pair that is presented in her schedule." Could be interpreted that she will come only to the first pair that actually has a class (i.e., the first '1'), but if the first pair is a class, she will be there; but if the first pair is a break, she sleeps and doesn't come. So it's as we thought.
Perhaps the rule about going home only if the break consists of at least two free pairs in a row is applied not only after a class but also when she is at the university during a break? For example, after pair2, she is waiting, but maybe she will then go home if the upcoming break after that waiting is long enough? But that seems odd because she is already at university; she might go home during a break if she's already there? The phrase "Alena has time to go home only if the break consists of at least two free pairs in a row" suggests that when there is a break (i.e., a sequence of consecutive zeros), she can go home for the duration of that break, but only if the break length >=2. But if she is already at university (e.g., waiting during a single zero), she cannot go home because the break is too short. That's consistent.
But maybe she also decides to go home before a break? For example, after pair3, there is a break of length 2, so she goes home. That we did.
What if after pair6, she goes home? But break is only one zero; she cannot.
Maybe the interpretation of "next pair" when she returns is ambiguous: After going home during a break, she returns to the university only at the beginning of the next pair. But which pair is that? For break from pair4 to pair5 (two zeros), the next pair after the break is pair6. So she returns at the start of pair6. But what about pair5? She's home. That's fine.
Now why is answer 4? Perhaps she does not count pair2 because she might go home after pair1? But break length is 1, so she cannot. But maybe if break length is exactly 1, she still can go home if she wants? No, rule says only if at least two.
Maybe the rule about "at least two free pairs in a row" refers to the total break length (including the pair she is currently in?) Let's think: If she just finished a class at pair i, then the next pair i+1 is a break. She might be at the university at the start of pair i+1. The break consists of consecutive zeros starting at i+1. If the length of that sequence is at least 2, then she can go home during that break. But if she stays, she would be present for pair i+1. But maybe she decides to go home immediately after the class, before the break starts, so that she is not present for any break. But then she would still be absent for pair i+1. The rule says she goes home when it is possible. So if break length >=2, she goes home. That's what we did.
But why does my count for sample2 yield 5? Let's enumerate carefully the expected answer 4. Which pairs are present? Possibly: 1,3,6,7? That's 4? Actually, that's 4: pairs 1,3,6,7. But we also had pair2 as present in our simulation. Did we erroneously count pair2? Let's see if maybe she doesn't stay at university for pair2. Could it be that after pair1, since break length is 1, she goes home? But the rule says she can go home only if the break is at least 2. So she cannot. So she must stay. But maybe the rule is that she goes home if there is a break of at least two free pairs, but if it's only one, she may still go home? No, "only if" means it's a necessary condition.
Perhaps the rule about "Alena has time to go home only if the break consists of at least two free pairs in a row" is to be applied to any break between classes, but maybe she only goes home if the entire break (i.e., the gap between two consecutive classes) is at least 2. In the case after pair1, the next class is pair3, gap length = 1 (only pair2). So she does not go home; she waits. That matches our simulation.
Thus pair2 should be present.
But answer 4 suggests pair2 is not present. So something else is off.
Maybe the output is the number of pairs during which she is present at the university, but they might be counting only the classes she attends? But sample1 output 4 includes pair3 which is a break, so not just classes.
Let's double-check sample1 output: They claim output 4. And in note they say: "In the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be at the university waiting for the next pair." So that confirms they count waiting.
Thus sample2 output 4 must be consistent with that.
Perhaps I mis-identified the first class. Could it be that she only comes to the first pair that is presented, but maybe "presented" means "scheduled" as in the first pair of the day? Actually, the phrase "first pair that is presented in her schedule" might be interpreted as the first pair that actually exists? No.
Maybe she only comes to the first pair that she has a class (i.e., first '1'), but if the first pair is a class, she might still not come because she wants to sleep as long as possible? But sleeping as long as possible would mean she comes as late as possible; if the first pair is a class, the latest she can come while still attending that class is at the start of that pair. So she would come.
Perhaps the schedule includes pairs that are not necessarily starting at fixed times? But that's unlikely.
Let's carefully re-read the problem statement for any nuance:
"Alena has successfully passed the entrance exams to the university and is now looking forward to start studying.
One two-hour lesson at the Russian university is traditionally called a pair, it lasts for two academic hours (an academic hour is equal to 45 minutes).
The University works in such a way that every day it holds exactly n lessons. Depending on the schedule of a particular group of students, on a given day, some pairs may actually contain classes, but some may be empty (such pairs are called breaks).
The official website of the university has already published the schedule for tomorrow for Alena's group. Thus, for each of the n pairs she knows if there will be a class at that time or not.
Alena's House is far from the university, so if there are breaks, she doesn't always go home. Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university.
Of course, Alena does not want to be sleepy during pairs, so she will sleep as long as possible, and will only come to the first pair that is presented in her schedule. Similarly, if there are no more pairs, then Alena immediately goes home.
Alena appreciates the time spent at home, so she always goes home when it is possible, and returns to the university only at the beginning of the next pair. Help Alena determine for how many pairs she will stay at the university. Note that during some pairs Alena may be at the university waiting for the upcoming pair."
I think the key phrase is: "Alena has time to go home only if the break consists of at least two free pairs in a row". This could be interpreted as: During a break (a sequence of consecutive zeros), if the length of that break is at least 2, then she has time to go home (i.e., she can make the round trip). However, she might not necessarily go home if she is already at the university? Actually, it says: "if there are breaks, she doesn't always go home. Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university." This suggests that if a break is at least 2 pairs long, she will go home. So when a break occurs (i.e., a sequence of zeros), she will go home for the entire break if its length >=2, and if length = 1, she will stay at the university waiting. But the nuance: When does she encounter a break? She encounters a break after a class (or after she returns to university?). The break is the interval between classes (or after last class). So the decision is made at the start of a break? Possibly, when she finishes a class, if the next pair is a break (0), then she will consider going home based on the length of the upcoming break. That is our interpretation.
Now, sample2: After pair3, there is a break of length 2, so she goes home. After pair6, there is a break of length 1, so she stays.
So why answer 4? Maybe the answer counts pairs where she is physically present, but they consider that if she goes home during a break of length >=2, she leaves at the beginning of the first zero pair and returns at the beginning of the pair after the break. That's what we assumed. So she is not present during any zero pairs of that break. So absent for 4 and 5.
Now present: 1,2,3,6,7 => that's 5.
Could it be that she is not present at pair7 because after pair6, she goes home? But break length is 1, so she cannot. Unless the rule is that she also goes home if there is a break after the last class regardless of length? The statement "if there are no more pairs, then Alena immediately goes home." That says if after some point there are no more pairs, she goes home. But after pair6, there is a pair7, so there is a pair, so not applicable.
Maybe the issue is about what counts as staying at the university during a pair: does she count if she is only present for part of the pair? Possibly she might leave immediately after a class if she can go home, and that might mean she is not present for the break that starts immediately after class? But the break is a separate pair. For example, after pair3, she goes home, which means she leaves at the end of pair3 (or beginning of pair4?) Actually, if she goes home, she would probably leave right after pair3 ends. The break pair(s) are separate time intervals. She is not present during those break pairs. So that's fine.
But maybe after pair6, even though there's a break of length 1, she might still go home because she can make it? No, the rule says only if break is at least 2.
Let's manually simulate the day with times. Each pair is 2 academic hours = 90 minutes. The break between pairs? Typically, pairs are back-to-back? The problem doesn't specify any gaps between pairs. They just say the university holds exactly n lessons. So I think the pairs are consecutive time slots. So if she wants to go home, she needs time to travel. She can only go home if there is at least a break of two consecutive pairs (i.e., 180 minutes) because the round trip takes time. That's the logic.
Thus in sample2, traveling home and back would take at least two free pairs. So after pair6, only one free pair, so she cannot go home. So she stays.
So something else must be happening to reduce the count to 4. Could it be that she doesn't attend pair2 because she might go home after pair1? But break length =1, can't. Unless the break length is measured including the pair she just finished? For example, maybe break consisting of at least two free pairs in a row means that there are at least two consecutive free pairs, and if she is currently at a class, that class is not part of the break. So after class, if the next two pairs are free, she can go home. That's what we did.
Maybe she also goes home if there is a break before the first class? But she is at home anyway.
Maybe the answer for sample2 is actually 5 but the problem statement says 4? Let's check the sample: Input:
7
1 0 1 0 0 1 0
Output:
4
Let's test with code mental: many people have solved this problem (likely from Codeforces). It's likely correct. So I must be misinterpreting something.
Perhaps the rule about when she goes home is not applied after every class, but rather she will go home at any time if she can, even during a break? But more importantly, maybe she only goes home at the beginning of a break, and if the break length is exactly 2, she can go home but then she might miss the next class? No, she returns at the beginning of the next pair. So that's fine.
Another possibility: After she returns from home for pair6, she arrives at the beginning of pair6. After pair6, there is a break of length 1. She might consider going home again, but the break is only 1, so she stays. That's correct.
Maybe she does not attend pair7 because after pair6, she leaves immediately? But that would contradict the rule.
Let's step through the day as a timeline:
Before pair1: at home.
Pair1: class. She attends. So present at pair1.
Between pair1 and pair2: she checks. The next pair is pair2 (0). The break length (consecutive zeros starting at pair2) = 1 (since pair3 is 1). Since <2, she stays at the university. So she is present at pair2.
Pair2: she is at university, waiting (no class). So present.
Between pair2 and pair3: she might consider? At the start of pair3, she has a class. She attends. So present at pair3.
After pair3, she checks: next pair4 =0, pair5=0, pair6=1 => break length=2. So she goes home. So she leaves the university after pair3 (or at the beginning of pair4). So she is absent for pair4 and pair5.
She returns at the beginning of pair6. So present at pair6.
After pair6, she checks: next pair7=0, then no more pairs. Break length = 1 (only pair7). Since <2, she stays at university. So present at pair7.
After pair7, no more pairs, she goes home.
Thus present: 1,2,3,6,7 = 5.
If answer is 4, maybe they are not counting pair2 as present because maybe she goes home after pair1? But we said cannot.
Let's consider if the break length is measured as the number of free pairs between classes. After pair1, the next class is at pair3, gap length = 1, so she cannot go home. That's what we have.
Maybe the rule is that she goes home only if the break consists of at least two free pairs, but she also must have time to go home and come back. That travel takes two pairs. So if the break is exactly two pairs, she can go home and come back. But maybe if it's exactly two, she can go home but she will miss the first of those two? Actually, if she goes home immediately after class, she may need some time to travel. Suppose a pair is 90 minutes. Travel home and back maybe takes 90 minutes each way? Actually, the problem doesn't specify travel time; it just says she has time to go home only if the break is at least two free pairs. That suggests the round trip takes at least the duration of two pairs (180 minutes). So if break length =2, she can go home, but she might be absent for both break pairs. So that's what we assumed.
But why does sample2 produce 5? Let's count again: perhaps they don't count pair1? If she sleeps as long as possible, maybe she oversleeps and misses the first class if it's too early? But the statement "she will sleep as long as possible, and will only come to the first pair that is presented in her schedule." Could be interpreted that she will only attend the first pair that is actually a class, but if the first pair is a class, she might not come because she might still be sleeping? That contradicts "sleep as long as possible" which suggests she will maximize sleep, but still attend classes? Actually, "sleep as long as possible" likely means she will not come to the university earlier than necessary. So she will arrive exactly at the start of the first class. So if the first pair is a class, she arrives at that time. So she attends.
Maybe the nuance: She will only come to the first pair that is presented in her schedule. That could be ambiguous: maybe "presented" means "not a break"? I think it's clear.
Let's examine the note for the first sample: They say: "In the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be it the university waiting for the next pair." So they state she stays from second to fifth inclusive. That's pairs 2,3,4,5 = 4. So indeed pair1 is not counted. But that's because pair1 is 0, so she didn't come. So that's consistent.
In second sample, according to our count, she stays from pair1 to pair3? Actually, from pair1 to pair3? She also stays at pair6 and 7. That's two intervals: [1,3] and [6,7]. So total pairs = 3 (1,2,3) + 2 (6,7) = 5. But answer says 4, so maybe one of these intervals is shorter.
Perhaps she does not stay at pair2? Could it be that after pair1, she goes home because break length is 1? But that would violate rule. Unless the rule is that she goes home if the break is at least one pair? But that would be contradictory to statement.
Maybe the rule about going home is applied not after every class, but only when she is at the university and there is an upcoming break of at least two free pairs. However, what about after she returns from home? At the start of pair6, she is at the university. At that moment, is there a break? No, it's a class. So she attends. After the class, there is a break. So she decides then.
Our simulation seems logical.
Maybe the mistake is that after pair3, she goes home and returns at the beginning of the next pair, but the next pair after the break is pair6. However, note that pair6 is a class. But maybe she returns at the beginning of the pair after the break, which is pair6, but she might only arrive at the beginning of that pair if she leaves home with enough time. The problem says "returns to the university only at the beginning of the next pair." So she will be there at the start of pair6. So present.
Now, why would answer be 4? Possibly they don't count pair7 because after pair6 she goes home immediately if there are no more classes? But there is a class at pair6, after that there is a break of one zero. But maybe they consider that if after the last class there are any zeros, she goes home immediately regardless of length? The statement "Similarly, if there are no more pairs, then Alena immediately goes home." could be misinterpreted: It might mean that if there are no more pairs that are classes (i.e., no more 1's), then she immediately goes home. But the phrasing "no more pairs" is ambiguous; it could mean no more pairs at all (i.e., end of schedule) or no more pairs with classes. However, the note in the first sample says: "In the first sample Alena stays at the university from the second to the fifth pair, inclusive". That implies after pair5 (which is a class), she goes home immediately even though pair5 is the last pair; there are no more pairs. So that's consistent with "if there are no more pairs" meaning end of day. In sample2, after pair6, there is a pair7, so there is a pair, so not end of day. So she wouldn't go home immediately.
But maybe after pair6, she checks: there is a break of one zero. Since it's only one, she cannot go home, so she stays. So present at pair7.
Unless the break is considered to start after pair6. If the break length is 1, she waits. So present.
I suspect that my interpretation of "first pair that is presented" might be wrong: perhaps she only comes to the first pair of the day (pair1) if it's a class, otherwise she sleeps through it? Actually, "first pair that is presented in her schedule" could be read as "first pair that actually occurs (i.e., the first scheduled event)". But that is always pair1. That would be weird.
Let's search for the original Codeforces problem. I recall a problem called "Sereja and Dima" but not this. There is a Codeforces problem "Alena And The Heater" maybe? Actually, I think this is Codeforces Round #381 (Div. 2) problem A? I'm not sure. Let's think: This problem is about breaks and staying at university. Possibly it's from Codeforces problem "Alena and the Schedule" or something.
Better to search memory: There is a problem "A. Alena's Schedule" maybe? I recall a problem with similar description: n up to 100, ai 0/1, count pairs where she is at university. I think I've seen it: Codeforces Round #394 (Div. 2) problem A? Not sure.
Anyway, the answer for second sample is 4, so my simulation must be off by 1. Let's try to compute manually using a different interpretation.
Interpretation 2: She only goes home when there is a break of at least two consecutive zeros, but she must be at the university at the beginning of the break to go home. However, if she is at home (after going home), she will return at the beginning of the next pair (the first pair after the break). That is same.
But what about the break after pair1? Pair2 is a zero, but it's only one zero, so she stays. That gave pair2 present. Maybe they consider that she doesn't stay at the university during pair2 because she might be at home if she went home after pair1? But rule prohibits that.
Unless the rule about "at least two free pairs" applies to any break, but maybe if a break is exactly one zero and it's between two classes, she still can go home because she could skip that one break? But that would contradict "only if". So no.
Maybe the counting of pairs at the university includes only pairs where she is physically present for the entire pair. If she goes home during a break, she leaves immediately after the previous class and may not be present for the break, but also may not be present for the class if she leaves early? No.
Another angle: Perhaps after she goes home for a break of length >=2, she does not return for the next class if the break ends exactly at the class? She returns at the beginning of the next pair, which is the class, so she is present.
Now, maybe the catch is that after pair3, she goes home. But she returns at the beginning of the next pair after the break, which is pair6. However, what about pair5? It's a zero. She's home. That's fine. But does she maybe return for pair5 if she leaves home earlier? No.
So present: 1,2,3,6,7. That's 5.
But answer 4 suggests maybe pair7 is not counted. Could it be that after pair6, she goes home because the break is at least one? No.
Or perhaps after pair6, there is a break of length 1, but since there are no more classes after that break (pair7 is 0 and then end), she considers that as "no more pairs"? Actually, after pair6, the next pair is pair7. After pair7, there are no more pairs. So if she stays for pair7, then after pair7 she goes home. So that would count pair7 as present.
But maybe the rule is that she only stays at the university for pairs that are either classes or breaks of length 1 that are between classes, but if a break is at the end (after the last class), she does not stay even if it's length 1. That is, she goes home immediately after the last class, regardless of trailing zeros. The statement "Similarly, if there are no more pairs, then Alena immediately goes home." could be interpreted as: If there are no more pairs (with classes) after the current class, she goes home immediately after that class. But the phrasing "no more pairs" is ambiguous; it might mean no more pairs in the schedule (i.e., the day ends). But if there are trailing zeros, those are pairs; they exist, so there are more pairs. However, they are breaks, not classes. But they are still pairs (lessons, though empty). So it says "the university holds exactly n lessons" - each pair is a lesson slot, even if empty. So they are pairs. So "no more pairs" would mean i = n, after the last pair. So if last class is at position L < n, then after L, there are more pairs (from L+1 to n). So she wouldn't go home immediately because there are more pairs. But the note for sample1: last class at 5, which is n=5, so she goes home immediately. That's fine.
But in sample2, last class is at 6, and there is a trailing zero at 7. So after class 6, there is a pair 7. So according to that, she doesn't go home immediately. So still present at 7.
Maybe the rule is that she goes home immediately after the last class, even if there are trailing breaks, because those breaks are not "pairs" in the sense of something she cares? But the problem explicitly says the university holds exactly n lessons; breaks are empty pairs. So they are pairs. So I'd think they count.
Let's test sample2 with the alternative: if she goes home immediately after the last class (pair6), then she would not be present for pair7. Then present pairs: 1,2,3,6 = 4. That matches output. So maybe that's the intended interpretation: After the last class (i.e., after the final 1 in the schedule), she goes home immediately, regardless of trailing zeros. And the phrase "if there are no more pairs" might refer to no more classes? But the wording: "Similarly, if there are no more pairs, then Alena immediately goes home." In the context, it might be that when she checks for the next pair after a class, if there is no next pair (i.e., she is at the last pair of the day), she goes home. But if there is a next pair but it's a break, she doesn't go home unless the break is long enough. That's what we did. However, the phrase "if there are no more pairs" could be read as "if there are no more pairs at all", meaning end of schedule. That's what we did. So after class at pair6, there is pair7, so there is a next pair; so she wouldn't go home immediately. So to get output 4, they must be ignoring pair7. So maybe they treat trailing breaks as not counting for staying because she goes home immediately after the last class, even if there are trailing breaks. The problem statement might be ambiguous, but the sample suggests that after the last class, she goes home immediately, not waiting for any trailing zeros. The phrase "if there are no more pairs" might be interpreted as "if there are no more pairs with classes"? Let's check the original Russian version? Possibly lost in translation.
I recall a Codeforces problem "Alena and the Heater"? No.
Let me search memory: There is a problem "A. Oleg and shares" no.
Actually, I recall a problem named "Alena And The Heater"? Not.
Wait, I think it's Codeforces Round #386 (Div. 2), problem A. I'm not sure.
Better to think: Many solutions for such problem likely just count: start from first 1 to last 1, and count all 1's and also count zeros that are between ones (i.e., zeros that are not at the ends). Actually, let's test that idea: For sample1: first 1 at index2, last 1 at index5. Zeros between ones: index3 is zero between ones (since 2 and 4 are ones). So total count = number of ones (3) + zeros that are between ones (1) = 4. That matches.
For sample2: first 1 at index1, last 1 at index6. Zeros between ones: we need zeros that are not at the ends? Actually, zeros that lie between the first 1 and last 1, but also might be counted only if they are isolated zeros (i.e., zeros with ones on both sides? But what about consecutive zeros? In sample2, between first and last, we have indices: 2 (zero), 3 (one), 4 (zero), 5 (zero), 6 (one). The zeros between first and last: indices 2,4,5. But if we count all zeros between first and last, that would be 3 zeros. Then total would be ones count (there are three ones: indices 1,3,6) = 3, plus zeros between = 3, total 6, which is not 4.
But maybe they count only zeros that are isolated (single zeros) between ones? In sample2, zeros between: index2 is isolated (since between 1 and 1? Actually index2 is between index1 (1) and index3 (1) -> isolated zero. index4 and index5 are consecutive zeros (break length 2). Maybe they are not counted because she goes home during that break. So they would count only isolated zeros (length 1 breaks) between ones. And they do not count trailing zeros after last 1 (index7) or leading zeros before first 1 (none). So ones: indices 1,3,6 => 3. Isolated zeros: index2 (single) => 1. Consecutive zeros: index4,5 not counted. Trailing zero index7 not counted. Total = 4. That matches.
And for sample1: first 1 at 2, last 1 at 5. Zeros between: index3 is isolated zero (since between 2 and 4). So ones: indices 2,4,5 => 3, plus isolated zero index3 => total 4. No leading/trailing zeros. Works.
Third sample: n=1, a=[0]. No ones, so first? There is no first 1, so she doesn't come at all, answer 0. That also matches.
Thus the rule appears to be: Alena is present for all classes (1) from the first 1 to the last 1 inclusive, and also present for any break (0) that is between two classes (i.e., not at the beginning or end) and that is isolated (i.e., the break consists of exactly one zero). If the break consists of two or more consecutive zeros between classes, she goes home and is absent for those zeros. Also, leading zeros before first class and trailing zeros after last class are always absent.
But is this exactly consistent with the description? Let's verify the logic with this interpretation.
She arrives at the first class (first 1). So she is present for that class.
Between classes, if there is a gap of zeros. If the gap length = 1, she stays at university (present for that zero). If gap length >=2, she goes home and is absent for all zeros in that gap.
After the last class, she goes home immediately (not present for any trailing zeros).
Before the first class, she doesn't come, so absent.
That matches the examples. Also, after a class, if the next pair is another class (gap length 0), she stays (obviously present for that class). And if the gap length >=2, she goes home. The nuance: If there are multiple gaps, each is handled independently.
In our earlier simulation, we also had this except for trailing zeros: we thought she stays for trailing single zero. But this interpretation says she does not stay for any trailing zeros, regardless of length. And the description "if there are no more pairs, then Alena immediately goes home." might be intended to mean that after the last class, even if there are trailing zeros, she goes home because there are no more classes? But the phrase "no more pairs" is ambiguous. However, given the sample, it's clear that trailing zeros are not counted. So the correct rule: She goes home immediately after the last class, regardless of what follows. Because if there were trailing zeros, they are still pairs, but she leaves. The problem statement might have meant "if there are no more pairs that are classes" but they said "pairs" meaning the time slots? I think in the original problem, it's understood that after the last class, she goes home, even if there are breaks after, because there are no more classes. Let's read again: "Similarly, if there are no more pairs, then Alena immediately goes home." This is after describing that she will only come to the first pair that is presented. Possibly they mean: After she finishes attending a class, if there are no more pairs (with classes) left, she goes home. Because if there is a break after, she would still have to come back? Hmm.
Actually, let's examine the note in first sample: "In the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be it the university waiting for the next pair." They specifically say from second to fifth inclusive, which are indices 2-5. There is no pair6. So trailing zeros not applicable. In second sample, if the rule were that she goes home immediately after last class (index6) and not stay for index7, then she stays from index1 to index3 and index6 only? That would be indices 1,2,3,6 = 4. But they didn't provide a note.
I think it's safe to adopt the interpretation: She stays from the first class to the last class, but she may skip (be absent) for breaks that are at least two zeros long. And she does not stay for any zeros after the last class. And she does not stay for any zeros before the first class.
Thus the answer = number of 1's + number of 0's that are between the first and last 1 and are isolated (i.e., not part of a consecutive block of zeros of length >=2). Equivalently, we can simulate the process:
Initialize present = 0.
Find first index i where a[i] = 1. If none, answer 0.
Find last index j where a[j] = 1.
Now iterate from i to j inclusive. We'll have a flag or we can count as: all 1's are present. For zeros, we need to see if they are part of a block of consecutive zeros of length >=2 within [i,j]. If a zero is isolated (single zero between 1's), then present++; if it's in a block of >=2 zeros, then they are all absent. So we can iterate through the segment, tracking runs of zeros.
That yields answer.
But is that exactly what we simulated earlier? Let's test on sample2 with this method: i=1, j=6. Segment: [1,2,3,4,5,6] -> values: 1,0,1,0,0,1. We'll process:
1: present++ (1)
0: start of zero run. Look ahead: next is 1? Actually, after 0 at index2, next index3 is 1, so run length =1. So isolated => present++ (2)
1: present++ (3)
0 at index4: look ahead: next index5 is 0, then index6 is 1. So run length =2. This is a block of length >=2, so we skip both zeros. So no present for index4 and 5.
Then index6 is 1: present++ (4)
Total 4. That matches.
Thus algorithm: iterate from first 1 to last 1, count 1s, and for zeros, only count if they are isolated (i.e., the zero is not adjacent to another zero within the considered segment). But careful: a zero at the boundary? Since we only consider between first and last 1, the zeros at the edges of the segment are between ones? At i=first 1 and j=last 1, the segment starts and ends with 1, so zeros are always between ones. So it's fine.
Implementation: Find first = index of first 1 (0-index or 1-index). If none, print 0. Else, find last = index of last 1. Then iterate i from first to last. Use a variable count = 0. While i <= last:
If a[i] == 1: count++, i++.
Else (a[i]==0): find the length of consecutive zeros starting at i, say len. If len == 1, then count++ (she stays for that single zero). If len >=2, then skip (no count). Then i += len.
That's simple.
But what about zeros that are at the very beginning (before first 1) or at the end (after last 1)? They are ignored, as per problem.
Edge cases: When there is only one 1 and no zeros in between: e.g., n=3, a=[0,1,0]. first=2, last=2. Segment is just index2 (1). Count=1. Output 1. Is that correct? She comes to pair2 (class). After class, there are trailing zeros? Since last class is at 2, and there is a trailing zero at 3, but she goes home immediately after class? According to our interpretation, she does not stay for pair3. So present = 1. Also, if after class there is a leading zero? Actually before first class, she didn't come. So that's fine. But is that consistent with the description? Let's simulate: She comes to pair2 (class). After pair2, she looks: next pair3 is 0. But since there are no more pairs after? Actually there is pair3, so there is a next pair. However, break length = 1 (since only one zero then end). Since break length <2, she would stay at university for pair3, according to our earlier strict simulation. But then she would be present for pair3, count=2. But our algorithm says 1. Which one is correct? The problem's second sample suggests that trailing zeros after last class are not counted (since they gave output 4, not 5). But in that sample, trailing zero after last class was present in our earlier strict simulation but answer said no. So it seems they do not count trailing zeros even if break length 1. So the rule is: after the last class, she goes home immediately, regardless of trailing zeros. The phrase "if there are no more pairs" might be interpreted as "if there are no more pairs that are classes". Or maybe the schedule only includes pairs up to the last class? But it says exactly n lessons, and some may be empty. So trailing zeros are part of the schedule. But perhaps Alena leaves immediately after the last class because she doesn't care about breaks after; she only cares about classes. The problem statement: "Similarly, if there are no more pairs, then Alena immediately goes home." This might be referring to after the last pair of the day, not after the last class. However, the sample contradicts that if we consider trailing zeros as pairs. So maybe the intended meaning is that she goes home immediately after the last class, even if there are empty pairs afterwards. So we must adjust.
Let's test with a simple case: n=2, a=[1,0]. According to our algorithm, first=1, last=1, count=1. Output 1. What would be intuitive? She comes to first pair (class). After class, there is a break of one pair. Since it's only one, she might wait? But the problem's logic: she values time at home, but she only goes home if break is at least two. So she would wait at university for that break? Or does she go home because after the class there are no more classes? Many people would think she goes home after the class because the day is effectively over (no more classes). But the schedule says there is an empty pair after. Does she have to stay? The problem might consider that she doesn't need to stay because there are no more classes. However, the statement "if there are no more pairs, then Alena immediately goes home." Could be interpreted as "if there are no more pairs (with classes)"? Not sure. But the sample2 indicates that trailing zero after last class is not counted. So I'd adopt the rule: trailing zeros after the last class are not counted. Also leading zeros before first class are not counted.
Thus the algorithm stands.
But we need to confirm with another example: Suppose n=3, a=[1,0,1]. Then first=1, last=3. Segment: 1,0,1. Zero is isolated => count both 1s and the zero => total 3. Does that make sense? She comes to pair1, after pair1 there is a single zero break, she stays (present for pair2), then pair3 class. So present for 1,2,3 => 3. That seems plausible.
Now, what about a=[1,0,0,1]? first=1, last=4. Segment: 1,0,0,1. Zero block length=2 => she goes home, so not present for zeros. So count: 1 + 1 =2. Is that consistent? She comes to pair1, after class sees break of length 2, goes home, returns for pair4. So present at 1 and 4 => 2. That seems correct.
Now, what about a=[1,0,0,0,1]? block length=3, she goes home, so present only at the classes: 2.
Now, a=[1,0,1,0,0]? first=1, last=3? Actually last class at index3. trailing zeros after index3: indices4,5. According to rule, after last class, she goes home immediately, so not present for trailing zeros. So segment: 1,0,1. Zero isolated => count=3. So answer 3. Let's simulate: She comes to pair1, after pair1 break length? Next is 0 at pair2, then pair3 is class. Break length =1 => she stays for pair2. Then pair3 class. After pair3, there are trailing zeros, but she goes home immediately. So present: 1,2,3 => 3. Good.
Now, a=[0,1,0,0]? first=2, last=2. segment only index2, count=1. Sim: She comes to pair2, after class there are zeros at 3,4, but she goes home immediately, so present only at 2. That yields 1.
Now, consider a scenario where there is a break of length >=2 after a class but before another class, she goes home. And if there is a break of length >=2 at the end, she also goes home (since it's after last class). So only breaks of length exactly 1 that are between two classes count.
Thus the problem reduces to: Count all 1's. Also count zeros that are between two 1's and are isolated (i.e., the zero is not adjacent to another zero in the original array, when considering only the segment between first and last 1). But careful: what about zeros that are isolated but at the edge of a block? For example, a=[1,0,0,1,0,1]. first=1, last=6. Segment: 1,0,0,1,0,1. Here zeros: indices2-3 block of length2 => skip. index5 is zero isolated (between 1 at 4 and 1 at 6) => count. So answer = ones (1,4,6) =3 plus isolated zero at5 =>4. That matches the rule.
Now, can there be a case where there is a zero that is isolated but adjacent to a zero outside the segment? That doesn't matter.
Thus algorithm:
Read n, array a of length n.
Find first = first index with a[i]==1 (0-index). If none, print 0 and return.
Find last = last index with a[i]==1.
Initialize ans = 0.
Iterate i from first to last inclusive:
if a[i] == 1:
ans += 1
else:
// count zero only if it is a single zero between ones. But careful: Could be a zero that is part of a longer run. So we need to detect runs.
// So we can while loop: if a[i]==0, we find the length of consecutive zeros starting at i. Let len = number of consecutive zeros from i until a[j]==1 or j>last? But only within the segment; but since segment ends with 1, the run of zeros will be bounded by ones (or end of segment). However, since last is last 1, the segment ends with 1, so any zero run within segment will end before a 1. So we can compute len. If len == 1, ans += 1; else skip. Then i += len.
Print ans.
Time complexity O(n).
We should test on given samples.
Sample1: n=5, a=[0,1,0,1,1].
first=1 (index1 0-index? Let's use 0-index: indices 0..4: [0,1,0,1,1]. first 1 at index1, last at index4. Iterate:
i=1: a[1]=1 -> ans=1.
i=2: a[2]=0, check run: next a[3]=1 => len=1 -> ans=2.
i=3: a[3]=1 -> ans=3.
i=4: a[4]=1 -> ans=4.
Output 4. Good.
Sample2: indices 0..6: [1,0,1,0,0,1,0]. first=0, last=5? Actually last 1 at index5 (value1). Wait, the array: index0=1, index1=0, index2=1, index3=0, index4=0, index5=1, index6=0. So last=5. Iterate i=0: a[0]=1 -> ans=1.
i=1: a[1]=0, run from index1: next index2=1 => len=1 -> ans=2.
i=2: a[2]=1 -> ans=3.
i=3: a[3]=0, run: index4=0, index5=1 => len=2 -> skip, i+=2 -> i=5.
i=5: a[5]=1 -> ans=4.
i=6 is > last, stop. Output 4. Good.
Sample3: n=1, a=[0]. No 1, output 0.
Edge case: n=1, a=[1]. first=0, last=0. ans=1. Should she stay? She comes to the only class, then no more pairs, goes home. So present at that pair: 1. So output 1. That seems correct.
Edge case: n=2, a=[1,1]. first=0, last=1. ans=1+1=2. She comes to pair1, next is class, stays. After pair2, goes home. So present 2. Good.
Edge case: n=2, a=[1,0]. first=0, last=0. ans=1. As argued, she goes home immediately after class, not stay for break. Good.
Edge case: n=2, a=[0,1]. first=1, last=1. ans=1. She comes to pair2, present, then goes home. Good.
Edge case: n=3, a=[1,0,0]. first=0, last=0. ans=1. After class, there are two zeros, but since it's after last class, she goes home immediately, not staying for zeros. Good.
Edge case: n=3, a=[0,0,1]. first=2, last=2. ans=1. She comes to pair3, present, goes home.
Edge case: n=4, a=[1,0,0,1]. first=0, last=3. Segment: 1,0,0,1. Zero run len=2 -> skip. ans=2. She goes home during the break. Good.
Edge case: n=5, a=[1,0,1,0,1]. first=0, last=4. Segment: 1,0,1,0,1. Zeros: at index1 isolated? It is between ones (index0 and index2) -> len=1 -> count. index3 isolated between index2 and index4 -> len=1 -> count. So ans = 3 (ones) + 2 =5. That would mean she stays for all five pairs. Let's simulate: She arrives at pair1 (class). After pair1, break length=1 (pair2) -> stays. Pair2 present. Pair3 class present. After pair3, break length=1 (pair4) -> stays. Pair4 present. Pair5 class present. After pair5, no more pairs? Actually it's the last, so goes home. So present all five. That seems plausible. Good.
Now, what about a scenario with zeros at the edges but also inside: a=[1,0,0,1,0]. first=0, last=3? Actually last 1 at index3. index4 is trailing zero, not counted. Segment: 1,0,0,1. Zero run len=2 skip. ans=2. She goes home during the two zeros, then returns for class at index3. Good.
Now, scenario: a=[1,0,0,0,1,0,0]. first=0, last=4. Segment: 1,0,0,0,1. Zero run len=3 skip. ans=2.
All seems consistent.
Thus the algorithm is solid.
Now confirm that this matches the problem's description exactly, with the interpretation that she goes home immediately after the last class (ignoring trailing zeros). The phrase "if there are no more pairs, then Alena immediately goes home." Might be a bit ambiguous but sample clarifies.
Also, we should consider: If there is a break of length >=2 that is not between two classes but after the last class, she goes home anyway, so same.
Thus final answer: implement as described.
Implementation details:
Read n.
Read list a of ints.
Find first = -1, last = -1.
For i in range(n): if a[i]==1: if first==-1: first=i; last=i.
If first==-1: print(0) and return.
Initialize ans=0, i=first.
While i <= last:
if a[i]==1:
ans+=1
i+=1
else:
# count consecutive zeros
j=i
while j<=last and a[j]==0:
j+=1
len0 = j-i
if len0 == 1:
ans+=1
i=j
Print ans.
Alternatively, a simpler approach: sum all 1's, and add 1 for each zero that has a 1 on both sides (immediately before and after). But careful: If there are consecutive zeros, only the isolated ones count. So we could iterate from first+1 to last-1 and if a[i]==0 and a[i-1]==1 and a[i+1]==1, then it's isolated. But that works only if the isolated zero is exactly a single zero with ones on both sides. However, what about a pattern like 1,0,1,0,1? Both zeros are isolated and would be counted because each zero has 1 on left and right. So that works. But what about a pattern like 1,0,0,1? The zeros are not isolated; they don't have 1 on both sides for each zero individually. The first zero has left 1, right 0; second zero has left 0, right 1. So they are not counted. So that approach also works: count ones, then count zeros where a[i]==0 and a[i-1]==1 and a[i+1]==1. But need to ensure we only consider between first and last. However, if there is a zero that is isolated but with ones not necessarily immediately adjacent? Actually, isolated means exactly one zero, so its neighbors are ones. So condition is correct. But what about scenario where there are multiple isolated zeros separated by ones: 1,0,1,0,1. Both zeros have ones on both sides. So that works. So a simpler solution: total = sum(a). Then for i from 1 to n-2? Actually need to consider from index 1 to n-2, but only if there is a 1 somewhere? But careful: we should only consider zeros that lie between the first and last 1. However, if we check the condition a[i]==0 and a[i-1]==1 and a[i+1]==1, that automatically ensures the zero is between two ones, and thus lies between some first and last 1. But could there be a zero outside the range? For example, a=[0,1,0,1,0]. The zero at index0 has no left 1, so not counted; zero at index2 has left 1 (index1) and right 1 (index3) so it is counted? But index2 is between first and last? first=1, last=3, so index2 is between, yes. That zero is isolated? It is a single zero, so she would stay. Should she? According to our rule, yes, because after class at index1, there is a break of one zero, she stays, then class at index3. So count that zero. So condition works. Zero at index4 has left 1? a[3]=1, right? no right neighbor (i+1 out of bounds) so not counted, which is correct because it's trailing zero after last class. So the condition a[i-1]==1 and a[i+1]==1 automatically restricts to zeros that are between two ones and not at edges. However, what about case where there is only one 1? Then there are no zeros with ones on both sides, so sum of ones only, which is correct (since no zeros between ones). So we can simply do:
ans = sum(a)
for i in range(1, n-1):
if a[i] == 0 and a[i-1] == 1 and a[i+1] == 1:
ans += 1
But wait, does this correctly handle the case where there are consecutive ones? For example, a=[1,1,0]? Let's test: sum=2. For i=1: a[1]=1, not zero. i=2? n=3, range(1,2) => only i=1. So ans=2. But what is the correct answer? n=3: [1,1,0]. first=0, last=1? Actually last 1 at index1. trailing zero at index2. She comes to first class at index0, after class next is class at index1, so she stays for both classes. After last class (index1), she goes home, not staying for index2. So present: index0, index1 => 2. So ans=2 correct.
What about a=[1,0,1,1]? n=4. sum=3. For i=1: a[1]=0, a[0]=1, a[2]=1 => condition true, ans++ => 4. Is that correct? She should be present for index0 (class), after class break length=1 (index1) => stay, present index1, then class at index2, then class at index3, after last class go home. So present: 0,1,2,3 => 4. Correct.
What about a=[1,0,0,1]? sum=2. i=1: a[1]=0, a[0]=1, a[2]=0 -> condition false (since a[2] not 1). i=2: a[2]=0, a[1]=0, a[3]=1 -> false. So ans=2. Correct.
What about a=[1,0,1,0,1]? sum=3. i=1: 0 with neighbors 1 and 1 -> true => ans=4. i=2: a[2]=1 skip. i=3: 0 with neighbors 1 and 1 -> true => ans=5. So ans=5 correct.
What about a=[1,0,1,0,0,1]? sum=3. indices:0:1,1:0,2:1,3:0,4:0,5:1. i=1: neighbors 1 and 1 -> true => ans=4. i=2: 1 skip. i=3: a[3]=0, neighbors a[2]=1, a[4]=0 -> false. i=4: a[4]=0, neighbors a[3]=0, a[5]=1 -> false. So ans=4. That matches earlier calculation (4). Good.
What about a=[1,0,0,1,0,1]? sum=3 (1 at 0,3,5). i=1: 0 neighbors 1,0 -> false. i=2: 0 neighbors 0,1 -> false. i=3: 1 skip. i=4: 0 neighbors 1,1 -> true => ans=4. So ans=4. Let's simulate: first=0, last=5. Segment: 1,0,0,1,0,1. Zero block len2 at indices1-2 skip; zero at index4 isolated => count. So ones=3, plus isolated zero=1 =>4. Works.
Thus the simple condition works for all cases. But is there any scenario where a zero is between two ones but not directly adjacent? No, because zeros are between ones means there is a block of zeros. If block length >1, then none of the zeros have both neighbors =1, because internal zeros have zero neighbors, and the edge zeros have one neighbor 1 and the other zero. So they are not counted. If block length =1, then that zero has ones on both sides. So the condition precisely captures isolated zeros. And it automatically excludes zeros at the boundaries because one neighbor would be out of bounds or not 1. So indeed, we can just compute sum of a plus number of indices i where a[i]==0 and a[i-1]==1 and a[i+1]==1, for i from 1 to n-2. But careful: i from 1 to n-2 ensures i-1 and i+1 within range. This includes zeros that are between ones even if the ones are not necessarily the first or last? It's fine. Because if there is a zero that is between two ones but one of those ones is not the first or last? It's still between first and last anyway. So it's correct.
But does it count a zero that is between a 1 and a 1 but the 1 on the left might be after a preceding zero? That doesn't matter.
Thus a very simple solution:
Read n, list a.
total = sum(a)
for i in range(1, n-1):
if a[i] == 0 and a[i-1] == 1 and a[i+1] == 1:
total += 1
print(total)
We should test edge cases: n=1, range(1,0) empty, total = sum = 0 or 1. Works.
n=2, range(1,1) empty? range(1,1) is empty. So total = sum. For [1,0] => total=1, correct. For [0,1] => total=1, correct. For [1,1] => total=2, correct.
n=3, range(1,2) includes i=1 only. For [1,0,1] => total=2 + condition true =>3, correct. For [1,0,0] => total=1, condition false (a[1]=0, a[0]=1, a[2]=0) =>1 correct. For [0,1,0] => total=1, condition? i=1 a[1]=1 skip, total=1 correct. For [0,0,1] => total=1, condition? i=1 a[1]=0, neighbors a[0]=0, a[2]=1 -> false, total=1 correct. For [1,1,0] => total=2, condition false (i=1 a[1]=1) =>2 correct. For [0,1,1] => total=2, condition? i=1 a[1]=1 skip =>2 correct.
Thus this works.
But wait, consider a scenario where there is a 1 at the start and a 1 at the end, and zeros in between but some isolated zeros, but also maybe there is a pattern like 1,0,1,0,0,1. We already tested.
What about a scenario with leading zeros and then 1,0,1, and trailing zeros: e.g., [0,1,0,1,0]. sum=2. i=1: a[1]=1 skip; i=2: a[2]=0, neighbors a[1]=1, a[3]=1 => true => total=3; i=3: a[3]=1 skip; i=4? range(1,4) for n=5? range(1,4) includes i=1,2,3. i=2 we counted. So total=3. According to rule: first=1, last=3. Segment: 1,0,1. Zero isolated => count. So present: index1,2,3 => 3. But note there are trailing zero at index4, not counted. So correct.
Thus the simple approach is valid and easier.
But is there any case where a zero has neighbors 1 and 1 but is not between the first and last 1? That would mean there are ones beyond, but the zero is still between two ones. The condition that she stays for that zero depends only on the break being isolated and between classes. It doesn't matter if there are additional classes later; if it's an isolated zero between two classes, she stays. That is correct. For example, [1,0,1,0,1]. Both zeros satisfy the condition and are counted, and she stays for both.
Thus the answer can be computed as described.
However, we must be cautious: The condition a[i]==0 and a[i-1]==1 and a[i+1]==1 might count a zero that is between two classes even if those classes are not consecutive in time? But they are consecutive in the array, meaning there is no other class between them; but if there is a zero between two classes, that's exactly a break. So it's correct.
Thus we can implement.
Let's provide final solution in Python.
But for completeness, I'll also consider if there is any nuance about the first and last class when there are multiple classes but zeros at boundaries? Already handled.
Thus solution:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
a = list(map(int, data[1:1+n]))
total = sum(a)
for i in range(1, n-1):
if a[i] == 0 and a[i-1] == 1 and a[i+1] == 1:
total += 1
print(total)
But before finalizing, double-check the logic for a tricky case: n=4, a=[1,0,0,1] sum=2. i=1: a[1]=0, a[0]=1, a[2]=0 -> false; i=2: a[2]=0, a[1]=0, a[3]=1 -> false. total=2. Good.
Another: n=5, a=[1,1,0,1,1] sum=4. i=1: a[1]=1 skip; i=2: a[2]=0, a[1]=1, a[3]=1 => total=5; i=3: a[3]=1 skip; range(1,4) includes i=1,2,3. total=5. Let's simulate: classes at 1,2,4,5? Actually indices 0..4: [1,1,0,1,1]. She comes to index0 class, then index1 class (since consecutive), after index1, next is index2=0, then index3=1 => break length=1 => she stays for index2. So present at index0,1,2,3,4 => 5. Good.
Thus correct.
One more: n=5, a=[1,0,1,0,0] sum=2. i=1: 0 neighbors 1 and 1 => total=3; i=2: 1 skip; i=3: 0 neighbors 1 and 0 => false; i=4? range(1,4) includes i=1,2,3. So total=3. Sim: first=0, last=2 (since last 1 at index2). Segment: 1,0,1. Zero isolated => count. So present at 0,1,2 => 3. trailing zeros ignored. So total=3. Good.
All good.
Thus final solution is simple.
But let's confirm that this solution does not overcount in case of multiple ones with zeros in between but also zeros that have 1 on both sides but are not between the first and last 1? Actually, if there is a zero with 1 on both sides, then automatically there is a 1 on left and a 1 on right, so the left 1 could be before the first 1? No, the first 1 is the earliest 1, so any 1 left of a zero must be at or after first 1. Similarly, any 1 right of a zero must be at or before last 1? Actually, consider array: [0,1,0,1,0,1]. The zero at index2? Let's index: 0:0,1:1,2:0,3:1,4:0,5:1. The zero at index2 has left a[1]=1, right a[3]=1, so it's between two ones. First 1 is at index1, last 1 at index5, so zero at index2 is between first and last. Zero at index4 has left a[3]=1, right a[5]=1, also between. So both are counted. That's correct.
Consider array: [1,0,1,0,1,0]. Zero at index1: left 1, right 1 -> counted; zero at index3: left 1, right 1 -> counted; zero at index5 is trailing, not counted because i=5 not in range? n=6, range(1,5) includes i=1..4, so index5 not considered. So total sum=3 +2=5. Simulate: first=0, last=4? Actually last 1 at index4? Wait, a[4]=1, a[5]=0, so last 1 is index4. Segment: 1,0,1,0,1. Both zeros isolated => count both. So present indices 0,1,2,3,4 => 5. trailing index5 absent. Good.
Consider array: [1,0,1,0,1,0,1] n=7. sum=4. Isolated zeros at indices1,3,5? Let's check: indices:0=1,1=0 (neighbors 1 and1) -> counted;2=1;3=0 (neighbors1 and1) -> counted;4=1;5=0 (neighbors1 and1) -> counted;6=1. So total=4+3=7. That would mean she stays for all 7 pairs. Simulation: first=0, last=6. Seg: 1,0,1,0,1,0,1. Each break is length 1, she stays each time. So present all 7. That seems correct: she never goes home because breaks are too short. So total=7.
Now consider array: [1,0,0,1,0,1,0,0,1]. n=9. sum=4 (ones at 0,3,5,8). Check isolated zeros: i=1? 0 neighbors 1 and 0 => false; i=2? 0 neighbors 0 and 1 => false; i=4? a[4]=0 neighbors a[3]=1 and a[5]=1 -> true; i=6? a[6]=0 neighbors a[5]=1 and a[7]=0 -> false; i=7? a[7]=0 neighbors a[6]=0 and a[8]=1 -> false. So total=4+1=5. Simulate: first=0, last=8. Segment: 1,0,0,1,0,1,0,0,1. Zeros: block len2 at indices1-2 => skip; isolated zero at index4 => count; block len2 at indices6-7 => skip. So present: ones at 0,3,5,8 (4) + isolated zero at 4 =>5. That matches.
Thus algorithm robust.
Now, confirm that the problem statement's constraints: n up to 100, trivial.
Thus we can output answer.
But before finalizing, I want to ensure that the interpretation is indeed the intended one. The note for first sample mentions she stays from second to fifth inclusive, and the third pair she waits. That matches our logic. For second sample, our computed answer 4 matches the given output. So good.
Now implement.
Will write solve() function and call it.
We'll provide explanation in final answer.
Now, produce final output. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
a = list(map(int, data[1:1 + n]))
code
# count all classes (ones)
ans = sum(a)
# add isolated breaks (single zeros between two ones)
for i in range(1, n - 1):
if a[i] == 0 and a[i - 1] == 1 and a[i + 1] == 1:
ans += 1
print(ans)