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
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

struct TestData;
void solve(TestData& t);
void io_solve(TestData& t, FILE* input, FILE* output);
void test();

#if _WIN32
#	define DO_TESTS 1
#	if DO_TESTS
#		define DO_LOGS 1
#	else
#		define DO_LOGS 0
#	endif
#else
#	define DO_LOGS 0
#	define DO_TESTS 0
#endif

#if DO_LOGS
auto time_start = std::chrono::high_resolution_clock::now();

#	define DEB(...) do { \
	auto duration_from_start = std::chrono::high_resolution_clock::now() - time_start;	\
	auto micros = std::chrono::duration_cast<std::chrono::microseconds>(duration_from_start).count(); \
	fprintf(stderr, "# %lld.%06lld ", micros/1000000, micros%1000000); \
	fprintf(stderr, __VA_ARGS__); \
	fputc('\n', stderr); \
} while (0)
#else
#	define DEB(...) do {} while (0);
#endif

struct TestData {
	int k;
	std::vector<int> a;

	int64_t result;

	TestData() {}
};

int main() {
#if DO_TESTS
	test();
#else
	FILE* input = stdin;
	FILE* output = stdout;

#if _WIN32
	const char *file = nullptr;
	//file = "in/r3d-x_1_tak.txt";
	if (file != nullptr) input = fopen(file, "r");
	if (input == NULL) {
		input = stdin;
	} else {
# 	  if 0
		std::string out_file = file;
		out_file += "_out.log";
		output = fopen(out_file.c_str(), "w");
#	  endif
	}
#endif
	TestData t;

	io_solve(t, input, output);
#endif
	return 0;
}

void solve(TestData& t);

#if DO_TESTS

void test_files() {
	std::vector<std::string> files = {
		"in/pdf-0.in",
		"in/my-0.in",
		"in/my-1.in",
		"in/my-2.in",
		"in/my-3.in",
	};
	for (int i = 0; i < files.size(); ++i) {
		DEB("test %d %s", i, files[i].c_str());
		FILE* file = fopen(files[i].c_str(), "r");
		if (file == nullptr) {
			DEB("can not open %s", files[i].c_str());
			return;
		}
		//FILE* outfile = fopen("out.out", "w");
		FILE* outfile = stdout;

		TestData t;
		io_solve(t, file, outfile);
		fclose(file);
		if (outfile != stdout) fclose(outfile);

		std::string check_out = files[i];
		check_out[check_out.size()-2] = 'o';
		check_out[check_out.size()-1] = 'u';
		check_out += 't';

		FILE* expect_file = fopen(check_out.c_str(), "r");
		if (expect_file == nullptr) {
			DEB("test %d. can not open %s\n", i, check_out.c_str());
		} else {
			DEB("test %d. Expects", i);
			char buf[8*1024];
			for (int lines = 0; lines < 3; ++lines) {
				fgets(buf, sizeof(buf) -1, expect_file);
				buf[sizeof(buf) -1] = 0;
				char* end = strchr(buf, '\n');
				if (end != nullptr) *end = '\0';
				DEB("%s", buf);
			}
			fclose(expect_file);
		}
	}
	DEB("test_files done %d", (int) files.size());
}

void test() {
#	if DO_TESTS
	{
		std::string in_file = "in/my-3.";
		std::string out_file = in_file + "out";
		in_file += "in";

		int64_t result = 0;

		FILE* test_file = fopen(in_file.c_str(), "w");
		int size = 1000;
		fprintf(test_file, "%d 0\n", size);
		int max = 1000000;
		fprintf(test_file, "%d ", max);
		for (int i = 1; i < size; ++i) {
			fprintf(test_file, "%d ", 0);
		}
		result = max * (size-1);
		fprintf(test_file, "\n");
		fclose(test_file);

		test_file = fopen(out_file.c_str(), "w");
		fprintf(test_file, "%lld\n", result);
		fclose(test_file);
	}
#	endif //DO_TESTS

	test_files();

	/*
	std::vector<TestData> tests;
	for(int i = 0; i < tests.size(); ++i)
	{
		auto& t = tests[i];
		DEB("test %d %d", i, t.m);

		solve(t);

		if (t.expected != t.result) {
			DEB("test %d: t.expected %d != %d t.result", i, t.expected, t.result);
			continue;
		}

		DEB("test %d done %d", i, t.result);
	}*/
}
#endif

void io_solve(TestData& t, FILE *input, FILE *output) {
	//DEB("io_solve");
	int lenght;

	fscanf(input, "%d %d\n", &lenght, &t.k);
	t.a.resize(lenght);
	for (int d = 0; d < lenght; ++d) {
		fscanf(input, "%d ", &t.a[d]);
	}

	solve(t);

	fprintf(output, "%lld\n", t.result);
}

struct Heap {
    struct Element {
        int value;
        int heap_pos;
    };

    std::vector<Element> m_data;
    std::vector<int> m_heap; // stores indices into m_data

    bool less(int l, int r) {
        return m_data[l].value < m_data[r].value; // max-heap
    }

    void heap_swap(int i, int j) {
        std::swap(m_heap[i], m_heap[j]);
        m_data[m_heap[i]].heap_pos = i;
        m_data[m_heap[j]].heap_pos = j;
    }

    void sift_down(int heap_pos) {
        int n = m_heap.size();
        while (true) {
            int left = 2 * heap_pos + 1;
            int right = 2 * heap_pos + 2;
            int largest = heap_pos;

            if (left < n && less(m_heap[largest], m_heap[left]))
                largest = left;
            if (right < n && less(m_heap[largest], m_heap[right]))
                largest = right;

            if (largest == heap_pos) break;

            heap_swap(heap_pos, largest);
            heap_pos = largest;
        }
    }

    void sift_up(int heap_pos) {
        while (heap_pos > 0) {
            int parent = (heap_pos - 1) / 2;
            if (!less(m_heap[parent], m_heap[heap_pos])) break;

            heap_swap(heap_pos, parent);
            heap_pos = parent;
        }
    }

    void heap_make() {
        m_heap.resize(m_data.size());

        for (int i = 0; i < m_heap.size(); ++i) {
            m_data[i].heap_pos = m_heap[i] = i;
        }

        for (int heap_pos = m_heap.size() / 2 - 1; heap_pos >= 0; --heap_pos) {
            sift_down(heap_pos);
        }
    }

    int heap_size() {
        return m_heap.size();
    }

    int heap_top_id() {
        return m_heap.front();
    }

    void heap_pop() {
		m_data[m_heap[0]].heap_pos = -1;
		
		m_heap[0] = m_heap.back();
		m_data[m_heap[0]].heap_pos = 0;
		
		m_heap.pop_back();
        if (!m_heap.empty())
            sift_down(0);
    }

	void heap_add(int id) {
		m_heap.push_back(id);
		m_data[id].heap_pos = m_heap.size()-1;
		sift_up(m_data[id].heap_pos);
	}

    void increase(int id, int new_value) {
        m_data[id].value = new_value;
        sift_up(m_data[id].heap_pos);
    }

    void decrease(int id, int new_value) {
        m_data[id].value = new_value;
        sift_down(m_data[id].heap_pos);
    }
};

void solve(TestData& t) {
	int k = t.k;
	Heap heap;

	heap.m_data.resize(t.a.size());
	for (int i = t.a.size() - 1; i >= 0; --i)
		heap.m_data[i].value = t.a[i];

	heap.heap_make();

	int64_t added = 0;

	while (heap.heap_size() > 1) {
		const int top_id = heap.heap_top_id();
		heap.heap_pop();

		int64_t target = heap.m_data[top_id].value - k;
		int check_id = top_id-1;
		if (check_id >= 0) {
			auto& element = heap.m_data[check_id]; 
			//only check if that value is to less, since we check from maximum values
			if (element.value < target) {
				//missing +  = target
				//missing = target - element;
				int64_t missing = target - element.value;
				added += missing;
				//element.value = target;
				heap.increase(check_id, target);
			}
		}
		check_id = top_id+1;
		if (check_id < heap.m_data.size()) {
			auto& element = heap.m_data[check_id]; 
			//only check if that value is to less, since we check from maximum values
			if (element.value < target) {
				//missing +  = target
				//missing = target - element;
				int64_t missing = target - element.value;
				added += missing;
				//element.value = target;
				heap.increase(check_id, target);
			}
		}
	}

	t.result = added;
}