Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Once upon a time, there was a magical land with n artifacts. Each artifact has a mysterious power of a [ i ] . These
Once upon a time, there was a magical land with n artifacts. Each artifact has a mysterious power of ai
These artifacts are protected by a spell such that they can only be used in groups of a particular size. The spell is encoded in an array b of length m and with values, indicating the length of each group.
You should divide the artifacts into m nonoverlapping contiguous subarrays, with the first subarray having a length of b the second subarray having a length of b and so on and use each group of artifacts independently. It is guaranteed that the sum of all the elements in b is exactly n
Let's say that you sort the powers of a group of artifacts aLR in ascending order.
Then, the power of the artifact with the kth smallest value where k is the number of distinct powers in that group of artifacts is considered to be its magic value
You are told that a group of artifacts provides you with a level up factor equivalent to the sum of magic value of all m subarray's of artifacts.
You are allowed to reorder the elements in b in any way you want.
Find the maximum possible value of level up factor.
Input Format
The first line contains an integer, n denoting the number of elements in a
The next line contains an integer, m denoting the length of the array b described in the problem.
Each line i of the n subsequent lins where i n contains an integer describing ai It is given that ai describes the power of artifact i
Each line i of the m subsequent lines where i m contains an integer describing bi contains an integer describing ai It is given that bi describes the length of the ith set of artifacts to be used together.
Given function:
int GetAnswerint n int m vector a vector b
Example:
Sample input :
n m a b
Sample output :
Explanation :
In this sample, if we leave the array b as it is b we divide the array a into a and a where the number of the distinct elements k in both of them is so we will take the first element from each of them after we sort each one of them they are initially sorted as k thus the answer and if we also take b then we will obtain the same answer.
Sample input :
nm a b
Sample output :
Sample input :
n m a b
Sample output :
Explanation :
in this sample, each permutation of array b will give the maximum possible answer except for b
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started