#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int tab[1'000'007];
int main()
{
ios::sync_with_stdio(0);
cin.tie(nullptr);
int n, k, w = 0;
cin >> n >> k;
for (int i = 0; i < n; i++)
{
cin >> tab[i];
}
tab[n] = -1;
for (int j = 0; j < n; j++)
{
for (int i = 0; i < n; i++)
{
if (tab[i] < tab[i + 1])
{
if (tab[i + 1] - tab[i] > k and tab[i + 1] != -1)
{
//printf("W%d| %d %d\n", i, tab[i], tab[i + 1]);
w += (tab[i + 1] - k - tab[i]);
tab[i] = tab[i + 1] - k;
}
}
else// tab[i]>tab[i+1]
{
if (tab[i] - tab[i + 1] > k and tab[i + 1] != -1)
{
// printf("P%d| %d %d\n", i, tab[i], tab[i + 1]);
w += (tab[i] - k - tab[i + 1]);
tab[i + 1] = tab[i] - k;
}
}
}
}
cout << w;
}
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 | #include <iostream> #include <algorithm> #include <vector> using namespace std; int tab[1'000'007]; int main() { ios::sync_with_stdio(0); cin.tie(nullptr); int n, k, w = 0; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> tab[i]; } tab[n] = -1; for (int j = 0; j < n; j++) { for (int i = 0; i < n; i++) { if (tab[i] < tab[i + 1]) { if (tab[i + 1] - tab[i] > k and tab[i + 1] != -1) { //printf("W%d| %d %d\n", i, tab[i], tab[i + 1]); w += (tab[i + 1] - k - tab[i]); tab[i] = tab[i + 1] - k; } } else// tab[i]>tab[i+1] { if (tab[i] - tab[i + 1] > k and tab[i + 1] != -1) { // printf("P%d| %d %d\n", i, tab[i], tab[i + 1]); w += (tab[i] - k - tab[i + 1]); tab[i + 1] = tab[i] - k; } } } } cout << w; } |
English