---
# System prepended metadata

title: 2835. Minimum Operations to Form Subsequence With Target Sum
tags: [Leetcode, Hard, Math]

---

Link: https://leetcode.com/problems/minimum-operations-to-form-subsequence-with-target-sum/description/
## 思路
思路参考[这里](https://leetcode.com/problems/minimum-operations-to-form-subsequence-with-target-sum/solutions/3965489/c-python-check-from-big-to-small/)
if ```total < target```, return -1
check from big to small
let's say the current number is called ```num```
if the sum of all the number smaller than ```num```, we can definitely drop ```num```
if ```num<=target```, we surely use it
else, we need to split the current ```num``` into two ```num//2```
## Code
```python=
class Solution:
    def minOperations(self, nums: List[int], target: int) -> int:
        tot = sum(nums)
        if tot<target:
            return -1
        nums.sort()
        ans = 0
        while target:
            num = nums.pop()
            if tot - num >= target:
                tot -= num
            elif num <= target:
                tot -= num
                target -= num
            else:
                nums.append(num//2)
                nums.append(num//2)
                ans += 1
        return ans
```