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
/
B_Jay_Patel.c
86 lines (66 loc) · 1.53 KB
/
B_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
#include <stdio.h>
#include <string.h>
void swap(long long *x, long long *y) {
long long temp = *x;
*x = *y;
*y = temp;
}
void Heapify(long long cost[], long long n, long long i) {
long long largest = i;
long long l = 2*i + 1;
long long r = 2*i + 2;
if(l < n && cost[largest] < cost[l])
largest = l;
if(r < n && cost[largest] < cost[r])
largest = r;
if(largest != i) {
swap(&cost[i],&cost[largest]);
Heapify(cost, n, largest);
}
return;
}
void Build_Heap(long long cost[], long long n) {
long long start = n/2 - 1;
for(long long i = start; i >= 0; i--) {
Heapify(cost, n, i);
}
return;
}
long long Cost(long long cost[], long long m) {
long long ans = cost[0];
cost[0] = cost[m-1];
Heapify(cost, m-1, 0);
return ans;
}
int main() {
int n,k;
scanf("%d %d",&n,&k);
long long cost[n];
memset(cost,0,sizeof(cost));
for(int i = 0; i < n; i++) {
char s[151]={};
scanf("%s",s);
for(int j = 151; j >= 0; j--) {
if((s[j] >= 'a' && s[j] <= 'z') || (s[j] >= 'A' && s[j] <= 'Z'))
continue;
else {
long long num = 0, k = 1;
while(j >= 0 && s[j] >= '0' && s[j] <= '9') {
num += k*(s[j]-'0');
k *= 10;
j--;
}
cost[i] += num;
}
}
}
Build_Heap(cost,n);
long long ans = 0, m = n;
for(int i = 0; i < k; i++) {
ans += Cost(cost,m);
ans %= (1000000007);
m--;
}
printf("%lld",ans);
return 0;
}