Harsh, like usual, started studying 6 months before for his end semester examinations. He was going to complete his 8th revision of the whole syllabus, when suddenly Pranav showed up in his room with the last year's question paper for their algorithms course. This paper contains a problem which both of them couldn't solve. Frustrated he asked you for help. But you declined him and instead try to do this problem instead:
You are given an array $A_1,A_2,\dots,A_N$, a positive integer $K$, and a function $F(x)=\displaystyle\sum_{i=1}^{N}{\left|{\left({x-A_i}\right)^K}\right|}$. Find the smallest integer $x$ such that $F(x)$ is minimum.Input
- The first line contains two space-seperated integers , $N$ and $K$
- The second line contains $N$ space-seperated integers $A_1,A_2,\dots,A_N$. Output
In the first and only line print the smallest integer $x$ such that $F(x)$ is minimumConstraints
- $1 \leq N \leq {10}^{5}$
- $1 \leq K \leq {3}$
- $1 \leq A_i \leq {5}\times{10}^{4}$ for each valid $i$Sample Input 1
3 1
6 1 7
Sample Output 1
6
Explanation 1
$F(6) = \displaystyle\sum_{i=1}^{N}{\left|{\left({6-A_i}\right)^K}\right|} \\ F(6) = \left|{\left({6-6}\right)^1}\right| + \left|{\left({6-1}\right)^1}\right| + \left|{\left({6-7}\right)^1}\right| \\ F(6)  = 0 + 5+ 1 \\ F(6)  = 6 $
Here $6$ is the minumum value for $F(x)$ for any integer value of $x$.Sample Input 2
3 2
6 1 7
Sample Output 2
5
Explanation 2
$F(5) = \displaystyle\sum_{i=1}^{N}{\left|{\left({5-A_i}\right)^K}\right|} \\F(5) = \left|{\left({5-6}\right)^2}\right| + \left|{\left({5-1}\right)^2}\right| + \left|{\left({5-7}\right)^2}\right| \\F(5) = 1 + 16 + 4 \\F(5) = 21$ 
Here $21$ is the minumum value for $F(x)$ for any integer value of $x$.Sample Input 3
3 3
6 1 7
Sample Output 3
4
Explanation 3
$F(4) = \displaystyle\sum_{i=1}^{N}{\left|{\left({4-A_i}\right)^K}\right|} \\F(4) = \left|{\left({4-6}\right)^3}\right| + \left|{\left({4-1}\right)^3}\right| + \left|{\left({4-7}\right)^3}\right| \\F(4) = 8 + 27 + 27 \\F(4) = 62 $
Here $62$ is the minumum value for $F(x)$ for any integer value of $x$.

Starter code:

Solutions:
#dt = {} for i in x: dt[i] = dt.get(i,0)+1
import sys;input = sys.stdin.readline
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]

n,k = ip()
x = ip()
x.sort()
if k == 1:
    a = x[n//2]
    b = x[n//2-1]
else:
    s = sum(x)
    a = s//n
    b = a + 1
sa = sum([abs((a-i)**k) for i in x])
sb = sum([abs((b-i)**k) for i in x])
if sa < sb:
    print(a)
else:
    print(b)

# cook your dish here

# using the concept of binary search

def ret_sum(arr,k,x):
    s=0
    
    for i in arr:
        s+=abs((x-i)**k)
    return s
    
   
n,k=list(map(int,input().split()))
arr=list(map(int,input().split()))
start,end=min(arr),max(arr)
hash_arr={}
flag=0

while start<=end and flag==0:
    mid=(start+end)//2
    mid_prev=mid-1
    mid_next=mid+1
    if mid not in hash_arr:
        hash_arr[mid]=ret_sum(arr,k,mid)
    if mid_next not in hash_arr:
        hash_arr[mid_prev]=ret_sum(arr,k,mid_prev)
    if mid_next not in hash_arr:
        hash_arr[mid_next]=ret_sum(arr,k,mid_next)
    if hash_arr[mid_prev]>hash_arr[mid] and hash_arr[mid_next]>hash_arr[mid]:
        print(mid)
        flag=1
        break
    elif hash_arr[mid_next]<hash_arr[mid]:
        start=mid
    else:
        end=mid
    
    


# cook your dish here
def func(n):
	sum = 0
	if k==1:
		for i in l:
			sum+= abs(n-i)
		return sum 
	if k==2:
		for i in l:
			sum+= (n-i)**2
		return sum
	if k == 3:
		for i in l:
			sum+= (abs(n-i))**3
		return sum
def findPeakUtil(low,high,n):
	mid = low + (high-low)//2
	one = func(mid-1)
	two = func(mid)
	three = func(mid+1)
	# print(one,two,three,mid)
	if (mid==0 or one >= two) and (mid==n-1 or three >= two):
		return mid 
	elif (mid>0 and one < two):
		return findPeakUtil(low,(mid-1),n)
	else:
		return findPeakUtil(mid+1,high,n)

def findPeak(n): 
    return findPeakUtil(0, n - 1, n) 

(n,k) = map(int,input().split())
l = list(map(int,input().split()))
print(findPeak(max(l)+2))

import sys
import math
from collections import defaultdict,Counter

input=sys.stdin.readline
def print(x):
    sys.stdout.write(str(x)+"\n")

# sys.stdout=open("CP2/output.txt",'w')
# sys.stdin=open("CP2/input.txt",'r')

# m=pow(10,9)+7
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.sort(reverse=True)
avg1=sum(a)//n
avg2=avg1+1
m=a[n//2]
ans1=0
ans2=0
ans3=0
if k==1:
    for j in a:
        ans1+=abs(j-m)
        ans2+=abs(j-a[n//2-1])
        # ans2+=abs(j-avg1)
        # ans3+=abs(j-avg2)
    if ans1<=ans2:
        print(m)
    else:
        print(a[n//2-1])
elif k==2:
    for j in a:
        # ans1+=abs(j-m)*abs(j-m)
        ans2+=abs(j-avg1)*abs(j-avg1)
        ans3+=abs(j-avg2)*abs(j-avg2)
    if ans2<=ans3:
        print(avg1)
    else:
        print(avg2)
else:
    for j in a:
        # ans1+=pow(abs(j-m),3)
        ans2+=pow(abs(j-avg1),3)
        ans3+=pow(abs(j-avg2),3)
    if ans2<=ans3:
        print(avg1)
    else:
        print(avg2)


# cook your dish here
def f(x):
    sumi=0
    for i in li:
        sumi+=(abs((x-i)))**k
    return sumi
def binary_search(l,r):
    mid=(l+r)//2
    if(f(mid)<f(mid-1)):
        if(f(mid)<=f(mid+1)):
            return mid
        return binary_search(mid+1,r)
    return binary_search(l,mid-1)
l=input().split()
n=int(l[0])
k=int(l[1])
l=input().split()
li=[int(i) for i in l]
print(binary_search(0,5*(10**4)+5))


# cook your dish here
def f(x):
    sumi=0
    for i in li:
        sumi+=(abs((x-i)))**k
    return sumi
def binary_search(l,r):
    mid=(l+r)//2
    if(f(mid)<f(mid-1)):
        if(f(mid)<=f(mid+1)):
            return mid
        return binary_search(mid+1,r)
    return binary_search(l,mid-1)
l=input().split()
n=int(l[0])
k=int(l[1])
l=input().split()
li=[int(i) for i in l]
print(binary_search(0,5*(10**4)+5))