#include<bits/stdc++.h>

using namespace std;

int N;
long long result;
vector<long long> rooms;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> N;
    long long num;
    for(int i = 0;i < N;i++) {
        cin >> num;
        if (num != 0) {
            rooms.push_back(i);
            result += num;
        }
    }
    if (rooms.size() == 0) {
        result += N/2;
    } else {
        rooms.push_back(rooms[0] + N);
        for (int i = 0;i < rooms.size() - 1;i++) {
            int first = rooms[i];
            int second = rooms[i + 1];
            result += (second - first) / 2;
        }
    }

    cout << result;
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: