#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef short int sint;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<sint, sint> pss;
typedef vector<ll> vll;
typedef vector<int> vint;
typedef vector<sint> vsint;
typedef vector<char> vchar;
typedef vector<bool> vbool;
typedef vector<vbool> vvbool;
typedef vector<vll> vvll;
typedef vector<pll> vpll;
typedef vector<pii> vpii;
typedef vector<pss> vpss;
typedef vector<ull> vull;
typedef vector<vint> vvint;
typedef vector<vvint> vvvint;
typedef vector<vchar> vvchar;
typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;
#define rep(i, m, n) for (int i = (int)(m); i <= (int)(n); ++i)
#define res(i, m, n) for (int i = (int)(m); i >= (int)(n); --i)
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define all1(x) (x).begin()+1, (x).end()
int n, k;
void wyr(int& x, int& y, ll& ans)
{
int r = y - x;
//cout << r << '\n';
if (r > k)
{
ans += r - k;
x = y - k;
}
}
void wyk(vint& a, ll& ans)
{
rep(i,0,n-1)
{
wyr(a[i], a[i+1], ans);
res(j,i-1,0)
wyr(a[j], a[j+1], ans);
}
}
int main()
{
cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);
cin >> n >> k;
vint a(n);
rep(i,0,n-1)
cin >> a[i];
ll ans = 0;
wyk(a, ans);
reverse(all(a));
wyk(a, ans);
cout << ans << '\n';
return 0;
}
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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef short int sint; typedef long double ld; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<sint, sint> pss; typedef vector<ll> vll; typedef vector<int> vint; typedef vector<sint> vsint; typedef vector<char> vchar; typedef vector<bool> vbool; typedef vector<vbool> vvbool; typedef vector<vll> vvll; typedef vector<pll> vpll; typedef vector<pii> vpii; typedef vector<pss> vpss; typedef vector<ull> vull; typedef vector<vint> vvint; typedef vector<vvint> vvvint; typedef vector<vchar> vvchar; typedef vector<vpii> vvpii; typedef vector<vpll> vvpll; #define rep(i, m, n) for (int i = (int)(m); i <= (int)(n); ++i) #define res(i, m, n) for (int i = (int)(m); i >= (int)(n); --i) #define fi first #define se second #define all(x) (x).begin(), (x).end() #define all1(x) (x).begin()+1, (x).end() int n, k; void wyr(int& x, int& y, ll& ans) { int r = y - x; //cout << r << '\n'; if (r > k) { ans += r - k; x = y - k; } } void wyk(vint& a, ll& ans) { rep(i,0,n-1) { wyr(a[i], a[i+1], ans); res(j,i-1,0) wyr(a[j], a[j+1], ans); } } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); cin >> n >> k; vint a(n); rep(i,0,n-1) cin >> a[i]; ll ans = 0; wyk(a, ans); reverse(all(a)); wyk(a, ans); cout << ans << '\n'; return 0; } |
English