Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Subsequence Weighting Problem - CodeSprint2
- # O(n^2) solution (wasn't fast enough)
- # Author: Kevin Tham
- def solve(numElements, sequence, weights):
- W = {} # Contains the weights of increasing subsequences up to index j
- maxWeighting = 0
- for i in range(numElements):
- w = weights[i]
- W[i] = w
- for j in reversed(range(0, i)):
- isIncreasing = sequence[j] < sequence[i]
- if (W[j] + w > W[i] and isIncreasing):
- W[i] = W[j] + w
- if W[i] > maxWeighting:
- maxWeighting = W[i]
- return maxWeighting
- def main():
- testCases = int(raw_input())
- for testCase in range(testCases):
- numElements = int(raw_input())
- sequence = map(int, raw_input().split())
- sequenceWeights = map(int, raw_input().split())
- print solve(numElements, sequence, sequenceWeights)
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment