Naruto and Equal Split
Practice
3.5 (2 votes)
Medium
Problem
9% Success 412 Attempts 30 Points 1.5s Time Limit 256MB Memory 1024 KB Max Code

Naruto has an array A of size N (N is always \(even\)), \(i_{th}\) element of which has a weight \(W_{i}\) and price \(P_{i}\). He wants to divide the entire array into 2 equal halves (each of size N/2) such that the sum of average price per unit weight of the first half and that of the second half is maximum i.e.
say the indexes of elements in first half is {\(X_{1}\), \(X_{2}\), ...., \(X_{N/2}\)} and the indexes of elements in second half is {\(Y_{1}\), \(Y_{2}\), ...., \(Y_{N/2}\)}, then the goal is to maximize
\(\frac{\sum_{i=1}^{N/2}P_{X_{i}}}{\sum_{i=1}^{N/2}W_{X_{i}}}\)+\(\frac{\sum_{i=1}^{N/2}P_{Y_{i}}}{\sum_{i=1}^{N/2}W_{Y_{i}}}\).

NOTE
Each element should be used only once.

INPUT FORMAT
The first line of input contains a single integer T denoting the number of test cases.
The first line of each test case contains a single integer N which is the number of elements of array A.
The second line contains N space separated integers, \(i_{th}\) integer of which denotes the weight \(W_{i}\) of the element \(A_{i}\).
This is followed by another line containing N space separated integers, \(i_{th}\) integer of which denotes the price \(P_{i}\) of the element \(A_{i}\).

OUTPUT FORMAT
The output should contain the required answer correct to 6 decimal places.

CONSTRAINTS

  • \(1 \le T \le 10\)
  • \(1 \le N \le 50\)
  • \(1 \le W_{i} \le 500\)
  • \(1 \le P_{i} \le 500\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
58 votes
Tags:
Dynamic ProgrammingOpenApprovedMedium
Points:30
1 votes
Tags:
Dynamic ProgrammingAlgorithmsC++3D dynamic programming
Points:30
Tags:
Dynamic ProgrammingAlgorithms3 DimensionalMedium