He spends 3*20=$<<3*20=60>>60 on the shares.
He sells 10 for 10*4=$<<10*4=40>>40.
He sells the other 20-10=<<20-10=10>>10 for a higher valuation.
Their price was 3*2=$<<3*2=6>>6.
So he got 10*6=$<<10*6=60>>60 from those.
So in total, he got 60+40=$<<60+40=100>>100.
So his profit was 100-60=$<<100-60=40>>40.
#### 40