Created
August 25, 2017 20:16
-
-
Save rachitagrawal14/ce5b8e4d788ee08e1bf3ade24b8746c3 to your computer and use it in GitHub Desktop.
You are given an array of N elements and an integer K . Let f(i,j) denote sum of min( K, j-i+1 ) greatest elements of the subarray from i to j. You need to compute sum of f(i,j) over all subarrays .
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sub_lists(my_list): | |
subs = [[]] | |
for i in range(len(my_list)): | |
n = i+1 | |
while n <= len(my_list): | |
sub = my_list[i:n] | |
subs.append(sub) | |
n += 1 | |
return subs | |
t=int(raw_input()) | |
result=[] | |
for i in range(t): | |
summed=[] | |
k=raw_input() | |
k=k.split() | |
for p in range(len(k)): | |
k[p]=int(k[p]) | |
l=raw_input() | |
l=l.split(" ") | |
for p in range(len(l)): | |
l[p]=int(l[p]) | |
subsl=sub_lists(l) | |
for j in subsl: | |
flag=0 | |
while len(j)> 0 and flag != k[1]: | |
flag+=1 | |
summed.append(max(j)) | |
j.remove(max(j)) | |
result.append(sum(summed)) | |
if len(result)==2: | |
print result[0] | |
print result[1] | |
else: | |
print result[0] | |
Example
Input:
1
4 2
1 2 3 4
Output:
44
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output
For each test case, output a single line containing the answer for that test case modulo 10^9 + 7
Constraints
1 ≤ T ≤ 2
1 ≤ N ≤ 100000
1 ≤ K ≤ 100
1 ≤ Ai ≤ 10^9