#include <bits/stdc++.h>

using namespace std;

namespace std {
#ifndef LOCAL
#define cerr \
  if (0) cerr
#endif
}  // namespace std

int64_t prf[100005];
int a[100005];
int64_t dp[100005];

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  int64_t m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    prf[i] = prf[i - 1] + a[i];
  }
  deque<int> dq;
  memset(dp, 0x3f, sizeof(dp));
  const int64_t inf = dp[0];
  dp[0] = 0;
  for (int i = 1, j = 0; i <= n; i++) {
    while (j < i && prf[i] - prf[j] > m) {
      ++j;
    }
    while (!dq.empty() && dq.front() <= j) {
      dq.pop_front();
    }
    while (!dq.empty() && a[dq.back()] < a[i]) {
      dq.pop_back();
    }
    dq.push_back(i);
    int last = j;
    for (auto it : dq) {
      dp[i] = min(dp[i], dp[last] + a[it]);
      last = it;
    }
  }
  cout << (dp[n] == inf ? -1 : dp[n]);
  return 0;
}
