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
#include <iostream>
#include <vector>

using i64 = int64_t;
constexpr i64 inf = 1125899906842624;

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

  int n;
  std::cin >> n;

  std::vector<i64> elefac;
  std::vector<i64> dists;
  std::vector<i64> pos;
  std::vector<i64> sumele;

  elefac.push_back(0);
  dists.push_back(0);
  pos.push_back(0);
  sumele.push_back(0);

  i64 dist = 1;
  for (int i = 0; i < n; ++i) {
    i64 v;
    std::cin >> v;
    if (v != 0) {
      elefac.push_back(v);
      sumele.push_back(sumele.back() + v);
      pos.push_back(i);
      if (elefac.size() == 2) {
        dist = 1;
      } else if (elefac.size() > 2) {
        dists.push_back(dist);
        dist = 1;
      }
    } else {
      ++dist;
    }
  }

  int m = elefac.size() - 1;

  if (m == 0) {
    std::cout << 0 << std::endl;
    return 0;
  }

  std::vector<i64> best(m + 1, 0);
  std::vector<i64> ener(m + 1, 0);
  std::vector<i64> cost(m + 1, 0);
  std::vector<bool> valid(m + 1, false);

  cost[0] = 0;
  ener[1] = elefac[1];
  best[1] = 1;
  valid[0] = true;
  valid[1] = ener[1] > 0;

  i64 totene = elefac[1];

  for (int i = 2; i <= m; ++i) {
    i64 o1bst = i;
    i64 o1cst = cost[i - 1];
    i64 o1ene = elefac[i];
    bool o1val = valid[i - 1] && elefac[i] > 0;

    // connect to last best and reduce if possible
    totene += elefac[i];
    i64 o2bst = best[i - 1];
    i64 o2ene = ener[i - 1] + elefac[i];
    i64 o2cst = cost[i - 1] + dists[i - 1];
    bool o2val = totene >= 0;

    int bptr = i - 1;
    i64 segdist = 0;
    i64 segcene = elefac[i];
    while (best[bptr] > 0) {
      i64 segcst = pos[bptr] - pos[best[bptr]] + dists[bptr] + segdist;
      i64 segene = ener[bptr] + segcene;
      segdist = segcst;
      segcene = segene;

      if (cost[best[bptr]] + segcst < o2cst || o2ene < 0) {
        o2cst = cost[best[bptr]] + segcst;
        o2bst = best[bptr];
        o2ene = segene;
      }

      for (int p = best[bptr]; o2val && p < i; ++p) {
        segcst -= dists[p];
        segene -= elefac[p];
        if (!valid[p] || segene < 0)
          continue;
        if (cost[p] + segcst < o2cst || o2ene < 0) {
          o2cst = cost[p] + segcst;
          o2bst = p + 1;
          o2ene = segene;
        }
      }
      bptr = best[bptr] - 1;
      if (o2ene >= 0)
        break;
    }

    bptr = i;
    for (int p = i - 1; 0 < p; --p) {
      i64 segdist2 = pos[i] - pos[p + 1];
      i64 segcene2 = sumele[i] - sumele[p];

      if (!valid[p] || segcene2 < 0)
        continue;

      if (cost[p] + segdist2 < o2cst) {
        o2cst = cost[p] + segdist2;
        o2bst = p + 1;
        o2ene = segcene2;
      }
    }

    if (!o1val || o2cst < o1cst) {
      best[i] = o2bst;
      cost[i] = o2cst;
      ener[i] = o2ene;
    } else {
      best[i] = o1bst;
      cost[i] = o1cst;
      ener[i] = o1ene;
    }
    valid[i] = o1val || o2val;
  }

  std::cout << (ener[m] < 0 ? -1 : cost[m]) << std::endl;
  return 0;
}