Answered step by step
Verified Expert Solution
Link Copied!

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 a[i].
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 non-overlapping contiguous subarrays, with the first subarray having a length of b[1], the second subarray having a length of b[2], 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 a[L,R] in ascending order.
Then, the power of the artifact with the k-th 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
1) The first line contains an integer, n, denoting the number of elements in a.
2) The next line contains an integer, m, denoting the length of the array b described in the problem.
3) Each line i of the n subsequent lins (where 0<= i < n) contains an integer describing a[i]. It is given that a[i] describes the power of artifact i.
4) Each line i of the m subsequent lines (where 0<= i < m) contains an integer describing b[i]. contains an integer describing a[i]. It is given that b[i] describes the length of the ith set of artifacts to be used together.
Given function:
int GetAnswer(int n, int m, vector a, vector b){
}
Example:
Sample input 1:
1) n=5, m=2, a=[1,1,2,2,2], b=[2,3]
Sample output 1:
1)3
Explanation 1:
In this sample, if we leave the array b as it is b=[2,3], we divide the array a into a[1,2]=[1,1] and a=[3,5]=[2,2,2] where the number of the distinct elements k in both of them is 1, so we will take the first element from each of them after we sort each one of them (they are initially sorted) as k=1, thus the answer 1+2=3 and if we also take b=[3,2] then we will obtain the same answer.
Sample input 2:
1) n=7,m=3, a=[2,2,3,4,4,5,5], b=[2,2,3]
Sample output 2:
1)11
Sample input 3:
1) n=10, m=4, a=[1,2,3,3,3,3,3,3,3,3], b=[3,3,3,1]
Sample output 3:
1)12
Explanation 3:
in this sample, each permutation of array b will give the maximum possible answer except for b=[1,3,3,3]
Write its cpp code.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Databases Object Oriented Deductive Hypermedia Technologies

Authors: Kamran Parsaye, Mark Chignell, Setrag Khoshafian, Harry Wong

1st Edition

0471503452, 978-0471503453

Students also viewed these Databases questions