# Codeforces Round #387 (Div. 2), problem: (D) Winter Is Coming Solution in C/C++

Codeforces Round #387 (Div. 2), problem: (D) Winter Is Coming Solution in C/C++

```#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

int compare(const void *a, const void *b) {
int *pa = (int *) a;
int *pb = (int *) b;

return *pb - *pa;
}

int main() {
int n, k, i, head, tail, a, b;
static int aa[200000], bb[200000];

scanf("%d%d", &n, &k);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]);
a = b = 0;
for (i = 0; i < n; i++)
if (aa[i] < 0) {
else if (a > 0)
bb[b++] = a;
a = 0;
} else
a++;
tail = a;
printf("0\n");
else {
int m, c1, c2, c;

qsort(bb, b, sizeof(*bb), compare);
/* case 1: end winter tires */
c1 = 1;
for (i = 0; i < b && m > k; i++) {
m -= bb[i];
c1 += 2;
}
if (m > k)
c1 = INT_MAX;
/*case 2: end summer tires */
m = n - head - tail;
c2 = 2;
for (i = 0; i < b && m > k; i++) {
m -= bb[i];
c2 += 2;
}
if (m > k)
c2 = INT_MAX;
c = c1 < c2 ? c1 : c2;
printf("%d\n", c == INT_MAX ? -1 : c);
}
return 0;
}```
(Visited 84 times, 1 visits today)