-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpositivePrefixes.py
More file actions
41 lines (40 loc) · 951 Bytes
/
Copy pathpositivePrefixes.py
File metadata and controls
41 lines (40 loc) · 951 Bytes
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
def fun(n,k):
A=[]
if n&1:
for i in range(1,n+1):
if (i&1):
A.append(i)
else:
A.append(-i)
else:
for i in range(1,n+1):
if (not(i&1)):
A.append(i)
else:
A.append(-i)
sum=0
count=0
for i in range(len(A)):
sum+=A[i]
if sum>0:
count+=1
if count>k:
for i in range(len(A)-1,-1,-2):
if A[i]>0:
A[i]=-A[i]
count-=1
if count==k:
break
if count<k:
for i in range(len(A)-1,-1,-1):
if A[i]<0:
A[i]=-A[i]
count+=1
if count==k:
break
print(*A)
tc=int(input())
for _ in range(tc):
inp=list(map(int,input().split()))
n,k=inp[0],inp[1]
fun(n,k)