Here's what I'd do:
For each position generate a list of all possible pieces that can go there. Sort them by stats. Remove any overly expensive pieces from the lists. That is ones where there's another piece with higher or equal stats and lower cost.
To start with try the best possible piece in each position. If the score is under the limit then you're done.
If the score isn't under the limit then make sure that using the cheapest possible piece in each position gets you under the score limit. If it doesn't then it's unsolvable, otherwise this is your starting point for a search. If you don't have to put a piece in all positions then start with all positions empty.
What you need to find is the combination of substitutions that gives the biggest score increase, with a cost that keeps you within the limit. Unfortunately I believe that is the knapsack problem which is NP complete, so getting a perfect answer might take a while.
However you can use a greedy algorithm to get a quick but possibly inaccurate answer by repeatedly picking the best upgrade. That is you make the change that gives the biggest increase in stats per point of cost increase (while keeping within the cost limit).
This goes wrong in some cases. For example say you have 10 points spare, and only two possible changes left that fit the budget.
- Gain 10 stats for 2 points. 10/2 = 5 so this is the most cost effective change.
- Gain 20 stats for 10 points. 20/10 = 2 so this isn't very cost effective.
The ideal pick is of course the second one despite the inefficiency, because if you pick the first there's nothing you can do with those last 8 points.
However it's not too expensive when the number of options is low to just try them all (there's 2^N subsets to try). When the number of options is high, hopefully you aren't close enough to the cost limit for the result to benefit significantly from a full search.