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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    int cost = 0;
    int n;
    string strTowns;

    cin >> n; 
    getline(cin >> ws, strTowns);

    int* towns;
    towns = (int*)malloc(n * sizeof(int));

    int* fabs;
    fabs = (int*)malloc(n * sizeof(int));
    int numberFabs = 0;

    string town;
    istringstream f(strTowns);
    for (int i = 0; i < n; i++)
    {
        getline(f, town, ' ');
        int num = std::stoi(town);

        towns[i] = num;
        if (num < 0)
        {
            fabs[numberFabs] = i;
            numberFabs++;
        }
    }

    if (numberFabs == 0)
        cout << -1;

    for (int i = 0; i < numberFabs; i++)
    {
        int val = abs(towns[fabs[i]]);

        int maxLeft = fabs[i];
        int sumLeft = 0;
        int maxRight = fabs[i];
        int sumRight = 0;

        int lastIndexLeft = fabs[i];
        int lastIndexRight = fabs[i];

        while (sumLeft + sumRight < val)
        {
            if (maxLeft - 1 < 0 && maxRight + 1 > n)
            {
                cout << -1;
                return 0;
            }

            if (maxLeft - 1 < 0) {
                if (towns[maxRight + 1] > 0)
                    lastIndexRight = maxRight + 1;
                sumRight += max(towns[maxRight + 1],0);
                maxRight++;
            }
            else if (maxRight + 1 > n) {
                if (towns[maxLeft - 1] > 0)
                    lastIndexLeft = maxLeft - 1;
                sumLeft += max(towns[maxLeft - 1],0);
                maxLeft--;
            }
            else {
                if (towns[maxRight + 1] > 0)
                    lastIndexRight = maxRight + 1;
                if (towns[maxLeft - 1] > 0)
                    lastIndexLeft = maxLeft - 1;

                sumLeft += max(towns[maxLeft - 1], 0);
                sumRight += max(towns[maxRight + 1], 0);
                maxRight++;
                maxLeft--;
            }
        }

        if (lastIndexRight > lastIndexLeft) 
        {
            cost += lastIndexRight - fabs[i];
            //if (sumRight >= val)
            //{
                towns[maxRight] = sumRight - val;
                for (int j = maxRight - 1; j > fabs[i]; j--)
                {
                    towns[j] = 0;
                }
            //}
            if (sumRight < val)
            {
                int rest = val - sumRight;
                for (int j = fabs[i] - 1; j >= maxLeft; j--)
                {
                    if(towns[j] > 0)
                    {
                        rest = -towns[j];
                        towns[j] = min(rest, 0);
                        if(rest <= 0)
                        {
                            cost += fabs[i] - j;
                            break;
                        }
                    }
                }
            }
        }
        else
        {
            cost += fabs[i] - lastIndexLeft;
            //if (sumLeft >= val)
            //{
                towns[maxLeft] = sumLeft - val;
                for (int j = maxLeft + 1; j < fabs[i]; j++)
                {
                    towns[j] = 0;
                }
            //}
            if (sumLeft < val)
            {
                int rest = val - sumLeft;
                for (int j = fabs[i] + 1; j >= maxRight; j++)
                {
                    if (towns[j] > 0)
                    {
                        rest = -towns[j];
                        towns[j] = min(rest, 0);
                        if (rest <= 0)
                        {
                            cost += j - fabs[i];
                            break;
                        }
                    }
                }
            }
        }
    }
    cout << cost;
}