This repository has been archived by the owner on Jun 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
G_Jay_Patel.c
107 lines (82 loc) · 1.75 KB
/
G_Jay_Patel.c
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
#include <stdio.h>
#define abs(a) (a>0 ? a : -a)
int k = 1, sum = 0;
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
void Max_Heapify(int cost[], int n, int i) {
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;
if(l < n && cost[i] < cost[l])
largest = l;
if(r < n && cost[i] < cost[r])
largest = r;
if(largest != i) {
swap(&cost[i],&cost[largest]);
Max_Heapify(cost, n, largest);
}
return;
}
void Min_Heapify(int cost[], int n, int i) {
int smallest = i;
int l = 2*i + 1;
int r = 2*i + 2;
if(l < n && cost[i] > cost[l])
smallest = l;
if(r < n && cost[i] > cost[r])
smallest = r;
if(smallest != i) {
swap(&cost[i],&cost[smallest]);
Min_Heapify(cost, n, smallest);
}
return;
}
void Build_Min_Heap(int a[], int n) {
int start = n/2 - 1;
for(int i = start; i >= 0; i--) {
Min_Heapify(a, n, i);
}
return;
}
void Build_Max_Heap(int b[], int n) {
int start = n/2 - 1;
for(int i = start; i >= 0; i--) {
Max_Heapify(b, n, i);
}
return;
}
void fun(int a[], int b[], int n, int ans[], int sum) {
ans[k++] = sum - 2*a[0];
sum -= 2*a[0];
a[0] = abs(b[0]-a[0]);
b[0] = abs(b[0]-a[0]);
Max_Heapify(b,n-1,0);
Min_Heapify(a,n-1,0);
return;
}
int main() {
int n,q;
scanf("%d %d",&n,&q);
int a[n], b[n];
for(int i = 0; i < n; i++) {
scanf("%d",&a[i]);
sum += a[i];
b[i] = a[i];
}
Build_Min_Heap(a,n);
Build_Max_Heap(b,n);
int m = n, ans[n];
for(int i = 1; i < n; i++) {
fun(a,b,m,ans,sum);
m--;
}
for(int i = 0; i < q; i++) {
int x;
scanf("%d",&x);
printf("%d",ans[x]);
}
return 0;
}