#include <stdio.h>
void quickSort(int numbers[], int array_size);
void q_sort(int numbers[], int left, int right);
void quickSort(int numbers[], int array_size)
{
q_sort(numbers, 0, array_size - 1);
}
void q_sort(int numbers[], int left, int right)
{
int pivot, l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = numbers[left];
while (left < right)
{
while ((numbers[right] >= pivot) && (left < right))
right--;
if (left != right)
{
numbers[left] = numbers[right];
}
while ((numbers[left] <= pivot) && (left < right))
left++;
if (left != right)
{
numbers[right] = numbers[left];
right--;
}
}
numbers[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
q_sort(numbers, left, pivot - 1);
if (right > pivot)
q_sort(numbers, pivot + 1, right);
}
int main()
{
int N, K, i, j = 1, k = 1;
long long result = 0;
int temp = 0, prev = 0;
int *arr;
scanf("%d %d", &N, &K);
arr = (int*)malloc(sizeof(int)*N);
for (i = 0; i < N; i++) {
scanf("%d", &arr[i]);
}
quickSort(arr, N);
for (i = 1; i <= K; i++) {
if (i % 2 == 1) {
if (i == 1) {
temp = arr[N - i];
result += temp;
}
else {
temp = arr[N - i + k];
result += temp - prev;
k++;
}
}
else {
prev = arr[i - (j + 1)];
j++;
}
}
printf("%lld", result);
}
Compilation message
wine.c: In function 'main':
wine.c:51:14: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration]
arr = (int*)malloc(sizeof(int)*N);
^
wine.c:51:14: warning: incompatible implicit declaration of built-in function 'malloc'
wine.c:51:14: note: include '<stdlib.h>' or provide a declaration of 'malloc'
wine.c:50:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &K);
^
wine.c:53:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &arr[i]);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
1116 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
1116 KB |
Output is correct |
5 |
Correct |
0 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
1116 KB |
Output is correct |
7 |
Correct |
0 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 ms |
1116 KB |
Output is correct |
9 |
Correct |
0 ms |
1116 KB |
Output is correct |
10 |
Correct |
0 ms |
1116 KB |
Output is correct |
11 |
Correct |
0 ms |
1116 KB |
Output is correct |
12 |
Correct |
0 ms |
1116 KB |
Output is correct |
13 |
Correct |
0 ms |
1116 KB |
Output is correct |
14 |
Correct |
0 ms |
1116 KB |
Output is correct |
15 |
Correct |
9 ms |
1116 KB |
Output is correct |
16 |
Correct |
0 ms |
1116 KB |
Output is correct |
17 |
Correct |
316 ms |
1116 KB |
Output is correct |
18 |
Correct |
329 ms |
1116 KB |
Output is correct |
19 |
Correct |
6 ms |
1116 KB |
Output is correct |
20 |
Correct |
6 ms |
1116 KB |
Output is correct |
21 |
Execution timed out |
1000 ms |
1508 KB |
Execution timed out |
22 |
Halted |
0 ms |
0 KB |
- |