program rrr(input,output); const inf=; type pointer=^nodetype; nodetype=record t,c:longint; next,rev:pointer; end; var a,cur:..]of pointer; d,q:..]of longint; p:pointer; i,n,m,s,t,c,x,y,ans:longint; function min(a,b:longint):longint; begin if a<b the…