I'm going to assume you know how to filter vertices "A" and "B" and start the traversal with:
gremlin> g = new TinkerGraph()
==>tinkergraph[vertices:0 edges:0]
...
gremlin> g.v("A","B").outE.groupBy{it.outV.next()}{[it.amount,it.inV.next()]}.cap.next()
==>v[A]=[[200, v[C]], [100, v[B]], [300, v[M]]]
==>v[B]=[[300, v[D]], [1300, v[L]], [200, v[E]]]
so the above gets you all the "A" and "B" elements grouped with their "amount" (not sure what that number represents in your domain). Once you have that, just use a "reduce" closure to sort and pop-off the first two items in the list:
gremlin> g.v("A","B").outE.groupBy{it.outV.next()}{[it.amount,it.inV.next()]}{it.sort{a,b->b[0]<=>a[0]}[0..<2]}.cap.next()
==>v[A]=[[300, v[M]], [200, v[C]]]
==>v[B]=[[1300, v[L]], [300, v[D]]]