Question

In: Computer Science

Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the...

Can you please solve this using recursion/ dynamic programming? Any programming language is fine.

Wallace the Weightlifting Walrus is training for a contest where it will have to lift 1000 kg. Wallace has some weight plates lying around, possibly of different weights, and its goal is to add some of the plates to a bar so that it can train with a weight as close as possible to 1000 kg.

In case there exist two such numbers which are equally close to 1000 (e.g. 998 and 1002), Wallace will pick the greater one (in this case 1002).

Help Wallace the Weightlifting Walrus and tell it which weight it will have to lift.

Input

The first line of the input contains the number of plates n (1≤n≤1000). Each of the following n lines contains one positive integer less than or equal to 1000, denoting the weight of each plate.

Output

Output one integer, the combined weight closest to 1000.

Sample Input 1 Sample Output 1
4
900
500
498
4
1002
Sample Input 2 Sample Output 2
1
1
1

Solutions

Expert Solution

Solution :

Following is the dynamic programming approach to the problem.

Language is C++. I have written comments to explain it.

#include <bits/stdc++.h>
using namespace std;

//Weight Wallace has to lift
const int V = 1000;
//boolean array to check if Wallace can lift certain sum of weight
bool can[2*V+1];

int main() {
    int n, x;
    cin>>n;
    can[0] = 1;
    int dist = V, ans = 0;
    for (int i = 0; i < n; ++i) {
        cin>>x;
        for (int j = 2 * V - x; j >= 0; --j) {
            if (can[j]) {
                can[j + x] = 1;
                //if new weight is closer to V, update the ans
                if (abs(j + x - V) < dist) {
                    dist = abs(j + x - V);
                    ans = j + x;
                }
                //if value is equally closer, take the larger one
                else if (abs(j + x - V) == dist) {
                    ans = max(ans, j + x);
                }
            }
        }
    }
    cout<<ans;
    return 0;
}

Code demo :

Input 1 :

Output 1 :

Input 2 :

Output 2 :


Related Solutions

Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude <iostream>)
Discrete math 1) Using MatLab (any language is fine just say what language is used). function...
Discrete math 1) Using MatLab (any language is fine just say what language is used). function d=lemma_gcd(a,b) %This program will return the greatest common divisor for input variables a %and b where a and b are integers such that they are not both 0. It uses %Lemma 4.8.3 on page 225. %Note: gcd(a,b)=gcd(-a,b)=gcd(a,-b)=gcd(-a,-b), so a=abs(a); b=abs(b); %This following section sets up the arrays we will use to store the %changing values of our variables as a sequence. "c" will be...
3. Solve using probabilistic dynamic programming: I would like to sell my computer to the highest...
3. Solve using probabilistic dynamic programming: I would like to sell my computer to the highest bidder. I have studied the market, and concluded that I am likely to receive three types of offers: an offer of $200 with probability 2/7, and offer of $300 with probability 4/7, and an offer of $400 with probability 1/7. I will advertise my computer for up to three consecutive days. At the end of each of the three days, I will decide whether...
You are using ONLY Programming Language C for this: In this program you will calculate the...
You are using ONLY Programming Language C for this: In this program you will calculate the average of x students’ grades (grades will be stored in an array). Here are some guidelines to follow to help you out: 1. In your program, be sure to ask the user for the number of students that are in the class. The number will help in declaring your array. 2. Use the function to scan the grades of the array. To say another...
Purpose For this programming project, you will create an application that draws fractal-like shapes using recursion....
Purpose For this programming project, you will create an application that draws fractal-like shapes using recursion. The class that creates the graphics window for the display of the shapes is given to you (see below). What you have to do is create a set of classes for the three shapes that are displayed in the graphics window. Two of the shapes must be a Sierpinski triangle and an H-shape. The third shape is your choice, though it must be a...
Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic...
Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic array structure given dynarray.h and dynarray.c below The second ADT you'll implement for this assignment is a queue. For this assignment, the interface for the queue (i.e. the structures and the prototypes of functions a user of the queue interacts with) is already defined for you in the file queue.h. Your first task in this assignment is to implement definitions for the functions that...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
Recursion Discussions Question: Why would you want to use recursion? Concerning programming, when would you want...
Recursion Discussions Question: Why would you want to use recursion? Concerning programming, when would you want to use iterative vs. recursive programming? Where does coding fit in concerning the software process (i.e. its use in design, in coding, etc.)? Describe recursion problems, and why does it seem to fit with recursion? (e.g. nature, math, music, sports game, etc.)  
Solve the following problem by Dynamic Programming: Maximize z = (y1 + 2)^2 + y2 *...
Solve the following problem by Dynamic Programming: Maximize z = (y1 + 2)^2 + y2 * y3 + (y4 - 5)^2 subject to y1 + y2 + y3 + y4 <= 5 yi >= 0 and integer, i = 1, 2, 3, 4
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, ....
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, . . . , n] of positive integers, an integer K. Decide: Are there integers in A such that their sum is K. (Return T RUE or F ALSE) Example: The answer is TRUE for the array A = [1, 2, 3] and 5, since 2 + 3 = 5. The answer is FALSE for A = [2, 3, 4] and 8. Note that you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT