Given an array A of N integers. Your task is to write a program to find the maximum value of ∑arr[i]*i, where i = 0, 1, 2,., n  1.
You are allowed to rearrange the elements of the array.
Note: Since output could be large, hence module 10^{9}+7 and then print answer.
 
Example 1:
Input : Arr[] = {5, 3, 2, 4, 1}
Output : 40
Explanation:
If we arrange the array as 1 2 3 4 5 then 
we can see that the minimum index will multiply
with minimum number and maximum index will 
multiply with maximum number. 
So 1*0+2*1+3*2+4*3+5*4=0+2+6+12+20 = 40 mod(10^{9}+7) = 40
Example 2:
Input : Arr[] = {1, 2, 3}
Output : 8
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function Maximize() that takes an array (arr), sizeOfArray (n), and return the maximum value of an array. The driver code takes care of the printing.
Expected Time Complexity: O(nlog(n)).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{7}
1 ≤ A_{i} ≤ N

Source: geeksforgeeks | Difficulty: EASY
Tags: Sorting

Data structures

Starter code: #User function Template for python3



class Solution:

    def Maximize(self, a, n): 

        # Complete the function

      



Solutions:
class Solution:

	def Maximize(self, a, n):
		a = sorted(a)
		ans = 0
		for i in range(len(a)):
			ans += a[i] * i
		return ans % (10 ** 9 + 7)


class Solution:

	def Maximize(self, a, n):
		a.sort()
		count = 0
		mod = 10 ** 9 + 7
		for i in range(n):
			count = (count + a[i] * i) % mod
		return count


class Solution:

	def Maximize(self, a, n):
		a.sort()
		return sum((i * a[i] for i in range(n))) % 1000000007


class Solution:

	def Maximize(self, a, n):
		a.sort()
		s = 0
		for i in range(1, n):
			s += a[i] * i
		return s % (pow(10, 9) + 7)


class Solution:

	def Maximize(self, a, n):
		sum = 0
		a.sort()
		for i in range(len(a)):
			sum = sum + a[i] * i
		return sum % (10 ** 9 + 7)


class Solution:

	def Maximize(self, a, n):
		MOD = 1000000000.0 + 7
		a.sort()
		summ = 0
		for (i, num) in enumerate(a):
			summ += num * i
			summ = int(summ % MOD)
		return summ


class Solution:

	def Maximize(self, a, n):
		arr.sort()
		l = []
		for i in range(n):
			l.append(arr[i] * i)
		r = sum(l)
		return r % 1000000007


class Solution:

	def Maximize(self, a, n):
		a.sort()
		mod = 1000000007
		sum = 0
		for i in range(n):
			sum = sum % mod + arr[i] * i
		return sum % mod


class Solution:

	def Maximize(self, a, n):
		b = sorted(a)
		sm = 0
		for j in range(n):
			sm = sm + j * b[j]
		return sm % (pow(10, 9) + 7)


class Solution:

	def Maximize(self, a, n):
		a.sort()
		su = 0
		for i in range(n):
			su = su + arr[i] * i
		return su % (10 ** 9 + 7)


class Solution:

	def Maximize(self, a, n):
		b = sorted(a)
		count = 0
		for i in range(n):
			count += b[i] * i
		if count > 10 ** 9:
			count = count % (10 ** 9 + 7)
		return count


import math

class Solution:

	def Maximize(self, a, n):
		a.sort()
		sum = 0
		for i in range(n):
			sum = int((sum + a[i] * i) % 1000000007)
		return sum


class Solution:

	def Maximize(self, a, n):
		a.sort()
		prod = 0
		for i in range(n):
			prod += a[i] * i
		return prod % 1000000007


class Solution:

	def Maximize(self, a, n):
		a.sort()
		sum = 0
		i = 0
		while i < n:
			sum = sum + i * arr[i]
			i += 1
		return sum % (10 ** 9 + 7)


class Solution:

	def Maximize(self, a, n):
		a.sort()
		k = 0
		if n == 1:
			return 0
		elif n == 2:
			return arr[1]
		else:
			for i in range(0, n):
				k = a[i] * i + k
				i = i + 1
		return k % (10 ** 9 + 7)


class Solution:

	def Maximize(self, a, n):
		a.sort()
		ans = 0
		for (i, num) in enumerate(a):
			ans += i * num
		return ans % (10 ** 9 + 7)


class Solution:

	def Maximize(self, a, n):
		ans = []
		a.sort()
		for i in range(n):
			x = a[i] * i
			ans.append(x)
		return sum(ans) % (10 ** 9 + 7)


class Solution:

	def Maximize(self, a, n):
		a.sort()
		val = 0
		mod = 1000000007
		for i in range(n):
			val = (val + a[i] * i % mod) % mod
		return val


class Solution:

	def Maximize(self, a, n):
		a.sort()
		num = 0
		newarr = []
		for i in range(n):
			newarritem = a[i] * num
			newarr.append(newarritem)
			num += 1
		return sum(newarr) % 1000000007


class Solution:

	def Maximize(self, a, n):
		a.sort()
		num = 0
		ans = 0
		for i in a:
			ans = ans + i * num
			num += 1
		return ans % 1000000007


class Solution:

	def Maximize(self, a, n):
		arr.sort()
		result = 0
		mod = 10 ** 9 + 7
		for i in range(len(arr)):
			result = (result + arr[i] * i % mod) % mod
		return result


class Solution:

	def Maximize(self, a, n):
		a.sort()
		ans = 0
		mul = 0
		for i in a:
			ans = ans + i * mul
			mul = mul + 1
		return ans % (10 ** 9 + 7)


class Solution:

	def Maximize(self, a, n):
		a = sorted(a)
		ans = sum([i * a[i] for i in range(n)])
		return ans % (pow(10, 9) + 7)


class Solution:

	def Maximize(self, a, n):
		a.sort()
		res = 0
		for (idx, val) in enumerate(arr):
			res += idx * val
		return res % (10 ** 9 + 7)


class Solution:

	def Maximize(self, a, n):
		a = sorted(a)
		ans = 0
		i = 0
		while i < n:
			ans += a[i] * i
			i += 1
		return ans % (pow(10, 9) + 7)


import math

class Solution:

	def Maximize(self, a, n):
		a.sort()
		b = 0
		for i in range(n):
			b = b + a[i] * i
		return int(b % (math.pow(10, 9) + 7))


class Solution:

	def Maximize(self, a, n):
		summ = 0
		a.sort()
		for i in range(len(a)):
			summ += a[i] * i
		return summ % 1000000007


class Solution:

	def Maximize(self, a, n):
		mod = 1000000007
		ans = 0
		a.sort()
		for i in range(len(a)):
			ans = (ans + a[i] * i % mod) % mod
		return ans


class Solution:

	def Maximize(self, a, n):
		a.sort()
		sum = 0
		for i in range(n):
			sum += i * arr[i]
		return sum % (1000000000 + 7)


class Solution:

	def Maximize(self, a, n):
		sort = sorted(a)
		mul = 0
		for i in range(n):
			mul += sort[i] * i
		return mul % 1000000007


class Solution:

	def Maximize(self, a, n):
		a.sort()
		addition = 0
		for i in range(n):
			if a[i] > 0:
				addition += arr[i] * i
		result = addition % (10 ** 9 + 7)
		return result


class Solution:

	def Maximize(self, a, n):
		a.sort()
		a1 = 0
		for i in range(n):
			a1 = a1 + arr[i] * i
		return a1 % 1000000007


class Solution:

	def Maximize(self, a, n):
		vyshu = 0
		a.sort()
		for i in range(0, len(a)):
			vyshu = vyshu + arr[i] * i
		return vyshu % 1000000007