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
146
147
148
149
#include <iostream>
#include <string>
#include <vector>

using namespace std;

enum CITY_TYPE {
    SUPPLIER,
    NORMAL,
    RECEIVER,
    MOCK_SRC,
    MOCK_OUT,
};
string CITY_TYPE_NAME[] {
    "SUPPLIER",
    "NORMAL",
    "RECEIVER",
    "MOCK_SRC",
    "MOCK_OUT",
};

struct City{
    int position;
    int energy;
    int type; 
};
ostream& operator<<(ostream& os, const City& city) {
    return os << "position: " << city.position << "\n"
              << "  energy: " << city.energy << "\n"
              << "    type: " << CITY_TYPE_NAME[city.type] << "\n\n";
}

int findNearestSupplier(const int rec_pos, const vector<int> &sup, const City* cities) {
    int n_cost = (2 ^ 31) - 1;
    int n_sup_pos = -1;
    int cost;
    for (auto it = sup.begin(); it != sup.end(); ++it) {
        if (cities[*it].energy > 0) { //supplier has energy
            cost = rec_pos - *it;
            if (cost < 0) {
                cost = -cost;
            }
            if (cost < n_cost) {
                n_cost = cost;
                n_sup_pos = *it;
            }
        }
    }
    return n_sup_pos;
}

int main() {
    ios::sync_with_stdio(false);

    int n;
    int a;
    vector<int> sup; //positions of suppliers 1..n
    vector<int> rec; //position of receivers 1..n

    cin >> n;
    const int SOURCE = 0; //position of source
    const int OUTPUT = n + 1; //position of output
    City cities[n + 2];

    cities[SOURCE] = {SOURCE, 0, MOCK_SRC};
    cities[OUTPUT] = {OUTPUT, 0, MOCK_OUT};

    for (int i = 1; i <= n; ++i) {
        cin >> a;
        if (a > 0) {
            cities[i] = {i, a, SUPPLIER};
            sup.push_back(i);
            cities[SOURCE].energy += a;
        } else if (a < 0) {
            cities[i] = {i, -a, RECEIVER};
            rec.push_back(i);
            cities[OUTPUT].energy -= a;
        } else {
            //normal city
            cities[i] = {i, a, NORMAL};
        }
    }

    if (cities[SOURCE].energy < cities[OUTPUT].energy) {
        cout << "-1";
        return 0;
    }

    int n_sup_pos;
    int currentFlow = 0;
    int d;
    int totalCost = 0;
    int cost;
    int min_cost;
    int sup_selected;
    int rec_selected;
    while (currentFlow < cities[OUTPUT].energy) {
        min_cost = (2^31) - 1;
        sup_selected = -1;
        rec_selected = -1;
        //find min path
        for (auto it = rec.begin(); it != rec.end(); ++it) { //iterating over receivers positions
            if (cities[*it].energy > 0) { //receiver need energy
                n_sup_pos = findNearestSupplier(*it, sup, cities); //for receiver
                if (n_sup_pos > 0) { //found
                    cost = n_sup_pos - *it;
                    if (cost < 0) {
                        cost = -cost;
                    }
                    if (cost < min_cost) {
                        sup_selected = n_sup_pos;
                        rec_selected = *it;
                        min_cost = cost;
                    }
                } else {
                    // not found
                    if (currentFlow == cities[OUTPUT].energy) {
                        cout << totalCost;
                    } else {
                        cout << "-1";
                    }
                    return 0;
                }
            }
        }
        // find min path - end
        if (cities[sup_selected].energy < cities[rec_selected].energy) {
            d = cities[sup_selected].energy;
        } else {
            d = cities[rec_selected].energy;
        }
        if (currentFlow + d > cities[OUTPUT].energy) {
            d = cities[OUTPUT].energy - currentFlow;
        }
        cities[sup_selected].energy -= d;
        cities[rec_selected].energy -= d;
        currentFlow += d;
        totalCost += min_cost;
        // cout << "DEBUG: sup: " << sup_selected << " rec: " << rec_selected << " path cost: " << min_cost << " flow: " << d << "\n";
    }

    // for (int i = 0; i <= OUTPUT; ++i) {
    //     cout << cities[i];
    // }

    cout << totalCost;

    return 0;
}