/** author: shobonvip created: 2026.08.25 15:14:34 **/ #include using namespace std; //* ATCODER #include using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) #define all(v) v.begin(), v.end() template bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template T max(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template T min(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template T sum(vector &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } struct UnionFind{ int n; vector par; vector l, r, v; explicit UnionFind(int t): n(t), par(t,-1), l(t), r(t), v(t) { iota(all(l), 0); iota(all(r), 0); } void merge(int a, int b){ int x = find(a); int y = find(b); if (x == y) return; if (-par[x] < -par[y]) swap(x, y); chmin(l[x], l[y]); chmax(r[x], r[y]); v[x] = v[y]; par[x] += par[y]; par[y] = x; return; } int find(int x){ if (par[x] < 0) return x; return par[x] = find(par[x]); } }; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n; ll k; cin >> n >> k; vector a(n+2); rep(i,1,n+1) { cin >> a[i]; } UnionFind uf(n + 2); rep(i,0,n+2) { uf.v[i] = a[i]; } rep(i,0,n+1) { if (a[i] == a[i+1]) { uf.merge(i, i+1); } } rep(len,1,n+1) { for (int l=1; l<=n; l+=len) { int g = uf.find(l); int lft = uf.l[g]; int rgt = uf.r[g]; //cout << l << ' ' << g << ' ' << lft << ' ' << rgt << ' ' << len << endl; if (lft + len - 1 == rgt) { if (lft - 1 >= 0 && rgt + 1 <= n+1) { if (uf.v[lft-1] < uf.v[g] && uf.v[g] > uf.v[rgt+1]) { ll t = max(uf.v[lft-1], uf.v[rgt+1]); int tar = min(k / len, uf.v[g] - t); uf.v[g] -= tar; k -= ll(tar) * len; if (uf.v[lft-1] == uf.v[g]) { uf.merge(lft-1, g); g = uf.find(l); } if (uf.v[rgt+1] == uf.v[g]) { uf.merge(rgt+1, g); g = uf.find(l); } } } } } } ll ans = 0; rep(i,0,n+1) { //cout << uf.v[uf.find(i)] << ' '; if (uf.find(i) != uf.find(i+1)) { ans += abs(uf.v[uf.find(i)] - uf.v[uf.find(i+1)]); } } cout << ans << endl; }