Runtime 2536ms Solution
I\'m not good at English, so please bear with me.\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nRecord the max value of each pos
I\'m not good at English, so please bear with me.\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nRecord the max value of each position be selected, and the max value of the last two position is current result.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Negative number is useless, and I need one array is editable for every query, so use $$realNums$$.\n- Use dp for recording current number be selected, leading 3 zero is just to facilitate calculation.\n- Calculate for every query, new query just affect result backward.\n- Avoid TLE, four consecutive 0\'s would be break loop, it doesn\'t affect result backward.\n\n# Complexity\n- Time complexity: O(n * m)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n![\u622A\u5716 2024-06-05 \u4E0B\u53484.06.54.png](https://assets.leetcode.com/users/images/d195a5f7-0c34-48f9-a0be-287b65967bb2_1717574998.1025956.png)\n\n\n# Code\n```\nclass Solution {\n    func maximumSumSubsequence(_ nums: [Int], _ queries: [[Int]]) -> Int {\n        let mudulo = 1000000007\n        let numsCount = nums.count\n        var realNums = nums.map { $0 > 0 ? $0 : 0 }\n        var dp: [Int] = Array(repeating: 0, count: numsCount+3)\n        for i in 0 ..< numsCount {\n            dp[i+3] = realNums[i] + max(dp[i], dp[i+1])\n        }\n        var result: Int = 0\n        var countNoDiff: Int\n        var tmp: Int\n        for query in queries {\n            if query[1] > 0 || realNums[query[0]] > 0 {\n                countNoDiff = 0\n                realNums[query[0]] = query[1] > 0 ? query[1] : 0\n                for i in query[0] ..< numsCount {\n                    tmp = realNums[i] + max(dp[i], dp[i+1])\n                    if dp[i+3] == tmp {\n                        countNoDiff += 1\n                        if countNoDiff > 3 {\n                            break\n                        }\n                    } else {\n                        countNoDiff = 0\n                    }\n                    dp[i+3] = tmp\n                }\n            }\n            result = (result + max(dp[numsCount+2], dp[numsCount+1])) % mudulo\n        }\n        return result\n    }\n}\n```
['Swift']