Chef spent N days working really hard! He planned loads of tasks: as many as Ai tasks to do on the ith day! Chef's work was brutal, so he only managed to finish Bi tasks on the ith day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
Input
- The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
- The first line of each test case contains three integers — N, K, M — denoting the number of days, white and black buttons appropriately.
- The second line contains N space-separated integers A1, A2, … , AN, denoting the number of planned tasks.
- The third line contains N space-separated integers B1, B2, … , BN, denoting the number of completed tasks.
- The fourth line contains K space-separated integers C1, C2, … , CK, denoting the integers on white buttons.
- The fifth and last line contains M space-separated integers D1, D2, … , DM, denoting the integers on black buttons.
Output
- In a single line, output an integer — the minimum possible amount of uncompleted tasks.
Constraints
- 1 ≤ T ≤ 4
- 1 ≤ N, K, M ≤ 10^5
- 1 ≤ Bi ≤ Ai ≤ 10^5
- 1 ≤ Ci, Di ≤ 10^5
Subtasks
- Subtask N ≤ 10, K, M ≤ 5. Points: 30
- Subtask Original constraints. Points: 70
Sample 1:
1 4 2 2 5 7 6 1 3 3 1 1 6 3 1 4
3
Explanation:
Example case 1. In this example Chef goes through the following steps: Use black button 1 on the first day. Use black button 4 on the second day. Use white button 3 on the third day. The arrays A and B are now effectively changed to: 5 7 3 1 4 7 1 1 So he will have 3 uncompleted tasks.
Code(C++):-
Code(JAVA):-
import java.util.*;import java.lang.*;import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */class Codechef{ public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while(T-->0){ int n = sc.nextInt(); int k = sc.nextInt(); int m = sc.nextInt(); int[] arr = new int[n]; int[] brr = new int[n]; for(int i=0;i<n ;i++){ arr[i]= sc.nextInt(); } for(int i=0;i<n ;i++){ brr[i]= sc.nextInt(); } int[] res = new int[k+m]; for(int i=0;i<(k+m);i++){ res[i] =sc.nextInt(); } int[] diff = new int[n]; for(int i=0;i<n ;i++){ diff[i]= arr[i]-brr[i]; } Arrays.sort(diff); Arrays.sort(res); int i=n-1; int j=k+m-1; long f = 0; while(i>=0&&j>=0){ // System.out.println(diff[i]+" "+res[j]); if(diff[i]>=res[j]){ f +=diff[i]-res[j]; i--; j--; }else{ j--; } } if(i>0){ while(i>=0){ f+=diff[i]; i--; } } System.out.println(f); } }}
Recommended Post :-
- Swap the adjacent characters of the string
- Double the vowel characters in the string
- Character with their frequency
- Program to find the closest value
- Swap adjacent characters
- Double the vowel characters
- Check valid parenthesis
- Print the characters with their frequencies
- Find closest value
- Word Count
- Program of CaesarCipher
- Program to find the perfect city
- Annual Day | Tech Mahindra coding question
- Find the number of pairs in the array whose sum is equal to a given target.
Full C course:-
Key points:-
- How to set limit in the floating value in python
- What is boolean data type
- How to print any character without using format specifier
- How to check that given number is power of 2 or not
- How to fix limit in double and floating numbers after dot (.) in c++
- How to print a double or floating point number in scientific notation and fixed notation
- How to take input a string in c
- How to reduce the execution time of program in c++.
Cracking the coding interview:-
Array and string:-
Tree and graph:-
Hackerearth Problems:-
- Very Cool numbers | Hacker earth solution
- Vowel Recognition | Hackerearth practice problem solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
- Playing With Characters | Hackerrank practice problem solution
- Sum and Difference of Two Numbers | hackerrank practice problem solution
- Functions in C | hackerrank practice problem solution
- Pointers in C | hackerrank practice problem solution
- Conditional Statements in C | Hackerrank practice problem solution
- For Loop in C | hackerrank practice problem solution
- Sum of Digits of a Five Digit Number | hackerrank practice problem solution
- 1D Arrays in C | hackerrank practice problem solution
- Array Reversal | hackerrank practice problem solution
- Printing Tokens | hackerrank practice problem solution
- Digit Frequency | hackerrank practice problem solution
- Calculate the Nth term | hackerrank practice problem solution
Data structure:-
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
MCQs:-
0 Comments