0

You have 𝑛 items. Each object has a weight, the weight of the object numbered 𝑖 is equal to 𝑥_𝑖. You need to put them in a backpack that holds no more than 𝑆g. At the same time, you want the TOTAL WEIGHT of ALL items in the backpack to be as large as possible and at the same time it (the total weight) to be ODD. If you cannot put an odd weight, you will enter 0.

Input data The first line gives the number of available items 0≤𝑛≤25. The second line lists 𝑛 numbers—the weights of the objects. The weights of the items do not exceed 10^9. The third line contains the number 0≤𝑆≤10^18, a limit on the total weight of the items that will be placed in the backpack.

Output On the first line print the largest odd total weight of objects that can be put in a backpack.

**MY SOLUTION **

import sys

def max_odd_weight(n, weights, S):
    possible_sums = {0}  
  
    for weight in weights:
        current_sums = set(possible_sums)  
        for w in current_sums:
            new_sum = w + weight
            if new_sum <= S:
                possible_sums.add(new_sum)  

    max_odd_weight = 0
    for w in possible_sums:
        if w % 2 != 0 and w > max_odd_weight:
            max_odd_weight = w

    return max_odd_weight

n = int(sys.stdin.readline())
weights = list(map(int, sys.stdin.readline().split()))
S = int(sys.stdin.readline())

result = max_odd_weight(n, weights, S)
sys.stdout.write(str(result))

It's really fast BUT I have a problem passing tests (All correct but memory limit exceeded). Test data is hidden. How can I cut the usage of memory even more?

time limit for test - 3 seconds Memory limit per test - 256 megabytes input - standard input output - standard output

using SYS helped, but not much

17
  • Can you add sample input and output? And a link to the website? Commented May 11, 2024 at 21:34
  • Sure Examples Input 3 1 4 8 10 Output 9 Input 4 5 7 12 18 20 Output 19 Input 5 3 5 3 3 5 10 Output 9 Input 5 72 74 42 68 57 100 Output 99 Unfortunately, the site is my university's contest and is private, so I cannot provide any links Commented May 11, 2024 at 21:40
  • That's all examples that are visible btw Other tests are hidden My code passes 42 tests, no problems except memory usage 42 passed with 608 ms 262100 Kb Commented May 11, 2024 at 21:41
  • 1
    No, I cannot provide any links, cause its a private contest in my unicersity, sorry Commented May 11, 2024 at 22:01
  • 1
    Are you sure you should be asking about an ongoing contest though? Commented May 11, 2024 at 22:05

2 Answers 2

0

The constraints on the problem mean that there could be over 34 million possible sums, which won't fit in memory if you use the standard dynamic programming solution for 0-1 knapsack.

There are a limited number of items, though, so I suggest recursive backtracking instead, like this:

import sys

def max_weight(weights, start, limit, odd):
    result = None if odd else 0
    for i in range(start, len(weights)):
        w = weights[i]
        if w <= limit:
            testodd = not odd if (w%2)==1 else odd
            test = max_weight(weights, i+1, limit-w, testodd)
            if test != None:
                test += w
                if (result == None or test > result):
                    result = test
    return result

n = int(sys.stdin.readline())
weights = list(map(int, sys.stdin.readline().split()))
S = int(sys.stdin.readline())

result = max_weight(weights, 0, S, True)
sys.stdout.write(str(result))
Sign up to request clarification or add additional context in comments.

6 Comments

It's working good only in Input 5 3 5 3 3 5 10 Output 9 But with result = max_weight(weights, 0, S, False) it fine with any other open test results ( Input 3 1 4 8 10 Output 9 Input 4 5 7 12 18 20 Output 19 Input 5 72 74 42 68 57 100 Output 99) except Input 5 3 5 3 3 5 10 Output 9
Oh, yes, I made a mistake in propagating the oddness check. Fixed.
btw can you explain how to count if the solution will fit into memory/time limits?
Well, this solution uses very little memory. Yours takes memory for each possible sum. There can be 34M possible sums, so the 256 MB limit leaves you less than 10 bytes for each one. They can be pretty big numbers and you put each one into 2 sets or so -- much bigger than 10 bytes each.
You could simplify this by just choosing to include or exclude the element at the current index.
|
0

Separate the up to 25 numbers into two parts with up to 12 and 13 numbers. Compute and sort all possible sums for each part. Combine them with a saddleback search, i.e., go through one set of numbers ascendingly and the other descendingly so that the sum of the two sums stays just under S. For that annoying oddness thing, actually combine the even sums from one set with the odd sums from the other, both ways.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.