This problem has many ways to solve it,here I want to write something about my greedy solution,thouth I haven't confirm it.

My solution is,firstly,sort the number sequence P，then start from the smallest one (suppose b>a)，if it can be divided into B set with a number which hasn't been used,just put it into B set,otherwise if it can be divided into A set with a number which hasn't been used,just put it into A set,or puts NO and ends the program.

I hope to know someone who has the same idea with me and confirm it and share my solution with you!!!