You have n items. Each object has a weight, the weight of the object numbered i is equal to x_i. You need to put them in a backpack that holds no more than Sg. 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≤n≤25. The second line lists n numbers—the weights of the objects. The weights of the items do not exceed 10^9. The third line contains the number 0≤S≤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
-
Can you add sample input and output? And a link to the website?Aicody– Aicody2024年05月11日 21:34:45 +00:00Commented 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 linksKarlLa– KarlLa2024年05月11日 21:40:34 +00:00Commented 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 KbKarlLa– KarlLa2024年05月11日 21:41:57 +00:00Commented May 11, 2024 at 21:41
-
1No, I cannot provide any links, cause its a private contest in my unicersity, sorryKarlLa– KarlLa2024年05月11日 22:01:25 +00:00Commented May 11, 2024 at 22:01
-
1Are you sure you should be asking about an ongoing contest though?Unmitigated– Unmitigated2024年05月11日 22:05:00 +00:00Commented May 11, 2024 at 22:05
2 Answers 2
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))
6 Comments
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
Explore related questions
See similar questions with these tags.