Swapping Pairs
Practice
5 (1 votes)
Dynamic programming
2d dynamic programming
Algorithms
Problem
89% Success 704 Attempts 30 Points 5s Time Limit 256MB Memory 1024 KB Max Code

You are given \(N\) pairs \((A_{1}, B_{1}),\ (A_{2}, B_{2}),\ ...,\ (A_{N}, B_{N})\). You are also given an integer \(M\).

You can swap any pair, that is, for the \(i^{th}\) pair, \(A_{i}\) becomes \(B_{i}\) and \(B_{i}\) becomes \(A_{i}\). You have to apply this operation in such a way that, 

\(\sum_{i=1}^{N} A_i = M\)

Your task is to determine whether the conditions can be satisfied or not. If the condition can be satisfied, then print \(YES\), otherwise print \(NO\).

Input Format

  • The first line contains an integer \(T\) denoting the number of test cases. Description of each test case is as follows:
  • The first line of each test case contains two space-seperated integers \(N\) and \(M\).
  • The next \(N\) lines contains two space-seperated integers \(A_{i}\) and \(B_{i}\) denoting the \(i^{th}\) pair.

Output Format

For each test case, print \(YES\), if it is possible to satisfy the given condition, otherwise print \(NO\).

Constraints

\(1 \le T \le 100\)

\(1 \le N \le 1000\)

\(1 \le M \le 10000\)

\(1 \le A_{i}, \ B_{i} \le 1000\)

It is guaranteed that the sum of values of \(N\) over all test cases in the input does not exceed \(1000\).

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
13 votes
Tags:
AlgorithmsDynamic ProgrammingJavaMediumOptimizationPythonTwo dimensional
Points:30
13 votes
Tags:
2-D ArrayAlgorithmsApprovedDynamic ProgrammingDynamic programmingMedium
Points:30
3 votes
Tags:
AlgorithmsDynamic ProgrammingMathMatrixMediumTwo dimensional