#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
long long wyn=0, now;
cin >> n >> k;
int co[n];
for (int i=0;i<n;i++)
{
cin >> co[i];
}
while (true)
{
now=0;
for (int i=1;i<n;i++)
{
if (co[i]>co[i-1])
{
now+=max(0, co[i]-co[i-1]-k);
co[i-1]=max(co[i-1], co[i]-k);
}
else
{
now+=max(0, co[i-1]-co[i]-k);
co[i]=max(co[i], co[i-1]-k);
}
}
wyn+=now;
if (now==0)
{
break;
}
}
cout << wyn;
}
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 | #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); int n, k; long long wyn=0, now; cin >> n >> k; int co[n]; for (int i=0;i<n;i++) { cin >> co[i]; } while (true) { now=0; for (int i=1;i<n;i++) { if (co[i]>co[i-1]) { now+=max(0, co[i]-co[i-1]-k); co[i-1]=max(co[i-1], co[i]-k); } else { now+=max(0, co[i-1]-co[i]-k); co[i]=max(co[i], co[i-1]-k); } } wyn+=now; if (now==0) { break; } } cout << wyn; } |
English