#include <stdio.h>
int N, K;
void merge_sort(int array[], int left, int right);
void merge(int num[], int left, int mid, int right);
void merge_sort(int array[], int left, int right)
{
int mid;
if (left < right) {
mid = (left + right) / 2;
merge_sort(array, left, mid);
merge_sort(array, mid + 1, right);
merge(array, left, mid, right);
}
}
void merge(int array[], int left, int mid, int right)
{
int i, j, k, m;
i = left;
j = mid + 1;
k = left;
int *tempArray;
tempArray = (int*)malloc(sizeof(int)*N);
while (i <= mid && j <= right) {
if (array[i] < array[j]) {
tempArray[k] = array[i];
i++;
}
else {
tempArray[k] = array[j];
j++;
}
k++;
}
if (i > mid) {
for (m = j; m <= right; m++) {
tempArray[k] = array[m];
k++;
}
}
else {
for (m = i; m <= mid; m++) {
tempArray[k] = array[m];
k++;
}
}
for (m = left; m <= right; m++) {
array[m] = tempArray[m];
}
}
int main()
{
int 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]);
}
merge_sort(arr, 0, N - 1);
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 'merge':
wine.c:27:20: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration]
tempArray = (int*)malloc(sizeof(int)*N);
^
wine.c:27:20: warning: incompatible implicit declaration of built-in function 'malloc'
wine.c:27:20: note: include '<stdlib.h>' or provide a declaration of 'malloc'
wine.c: In function 'main':
wine.c:64:14: warning: incompatible implicit declaration of built-in function 'malloc'
arr = (int*)malloc(sizeof(int)*N);
^
wine.c:64:14: note: include '<stdlib.h>' or provide a declaration of 'malloc'
wine.c:63:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &K);
^
wine.c:66:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &arr[i]);
^
# |
결과 |
실행 시간 |
메모리 |
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 |
1380 KB |
Output is correct |
10 |
Correct |
0 ms |
1380 KB |
Output is correct |
11 |
Correct |
0 ms |
1380 KB |
Output is correct |
12 |
Correct |
0 ms |
1380 KB |
Output is correct |
13 |
Memory limit exceeded |
6 ms |
65536 KB |
Memory limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |