The cost basis is the total cost that you put into the stock, so don't get too wrapped up in exactly which purchase lot is which. Figure out your initial investment and the total of dividends that were re-invested over time. That's your total basis.
You said that you sold part of the stocks, so do what I wrote above, but allocate the basis up to the number of shares that you sold. The rest of your cost goes to the basis on what you're still holding toward a future sale.
In your case this last part probably doesn't matter, but you would at some point need to allocate long versus short term gains. That means that the last few buys are probably short-term and that part of the basis should be totaled separately. Since you're still holding some of the stock though, it seems like you probably won't need to worry about this. (The most recent buys you should probably allocated to what you're still holding so that it has a chance to get to long-term status.)