1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
#include <iostream>

using namespace std;

int main() {

    int n;
    int* tab;
    long long sumaL, sumaP;
    int droga = 0;
    bool elektrownia = false, fabryka = false;
    int l;

    cin >> n;

    tab = new int[n];

    for (int i = 0; i < n; i++) {
        cin >> tab[i];
        if (tab[i] > 0) {
            elektrownia = true;
        }
        else if (tab[i] < 0) {
            fabryka = true;
        }
    }
    if (fabryka == false || elektrownia == false) {
        cout << "-1" << endl;
    }
    else {
        for (int i = 0; i < n; i++) {
            if (tab[i] < 0) {
                int k = 0;
                sumaL = 0;
                sumaP = 0;
                while (sumaL + sumaP <= -tab[i] && sumaL <= -tab[i] && sumaP <= -tab[i]) {
                    k++;
                    if (i - k >= 0) {
                        sumaL += tab[i - k];
                    }
                    if (i + k < n) {
                        sumaP += tab[i + k];
                    }
                }
                k = 0;
                if (sumaL >= -tab[i]) {
                    while (tab[i] < 0) {
                        k++;
                        tab[i] += tab[i - k];
                        tab[i - k] = 0;
                    }
                    if(tab[i] > 0){
                        tab[i - k] = tab[i];
                        tab[i] = 0;
                    }
                    droga += k;
                }
                else if (sumaP >= -tab[i]) {
                    while (tab[i] < 0) {
                        k++;
                        tab[i] += tab[i + k];
                        tab[i + k] = 0;
                    }
                    if (tab[i] > 0) {
                        tab[i + k] = tab[i];
                        tab[i] = 0;
                    }
                    droga += k;
                }
                else if (sumaP + sumaL >= -tab[i]) {
                    if (sumaP > sumaL) {
                        swap(sumaP, sumaL);
                    }
                    while (tab[i] + sumaP < 0) {
                        k++;
                        tab[i] += tab[i - k];
                        tab[i - k] = 0;
                        droga++;
                    }
                    if (tab[i] > 0) {
                        tab[i - k] = tab[i];
                        tab[i] = 0;
                    }
                    k = 0;
                    while (tab[i] < 0) {
                        k++;
                        tab[i] += tab[i + k];
                        tab[i + k] = 0;
                        droga++;
                    }
                    if (tab[i] > 0) {
                        tab[i + k] = tab[i];
                        tab[i] = 0;
                    }
                }
            }
        }
        cout << droga << endl;
    }
    delete[] tab;

    return 0;
}