How to maximize the sum?

六眼飞鱼酱① 提交于 2019-11-30 10:40:08

We can reformulate the problem as follows:

We have a list of numbers and we can activate or deactivate the numbers. We want to find the maximum sum of activated numbers, where in each pass we can switch exactly k numbers.

For odd k, we could do the following: Activate the maximum number (if it is positive) and use the remaining (k-1) switches to switch any number twice, which will effectively leave the number in its previous state. Therefore, the maximum pass value is the sum of positive numbers.

For even k, this is slightly different since the number of activated numbers is always even. Therefore, we first find all positive numbers. Let the number of positive numbers be p. If p is even, then we are good and the sum of these numbers is the result. If p is odd, we have to check two cases: Remove the smallest positive number or add the largest non-positive number. The maximum of these two cases is the result.

Edit from comments:

For the special case where k=n, there are only two options: Either include all numbers or exclude all numbers. If the sum of numbers is greater than 0, this is the result. Otherwise, the result is 0.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!