program Kug; {$mode objfpc}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} Classes { you can add units after this }; Type TDat=Record Value:Longint; Pos :Longint; End; TTab=Array[0..2001] of TDat; TTotal=Array[0..2000*2000] of Longint; var Tab:Array[0..2001] of TTab; D:TTab; DD,Total:TTotal; Max,Count:Longint; procedure MergeSort(Const Row,Start,Stop : integer);overload; var Med,Rock1,Rock2,Loop : integer; begin Med := (Start + Stop + 1) div 2; if Med-Start>1 Then MergeSort(Row, Start, Med - 1); if Stop-Med>0 Then MergeSort(Row, Med, Stop); Rock1:=Start; Rock2:=Med; for Loop:=Start to Stop do if (Rock1=Med) or ((Rock2<=Stop) and (Tab[Row][Rock1].Value>Tab[Row][Rock2].Value)) then Begin d[Loop]:= Tab[Row][Rock2]; inc(Rock2); End Else Begin d[Loop]:= Tab[Row][Rock1]; inc(Rock1); End; For Loop:=Start to Stop do Tab[Row][Loop]:=d[Loop]; End; procedure MergeSort(var ADat:TTotal;const Start,Stop : integer);overload; var Med,Rock1,Rock2,Loop : integer; begin Med := (Start + Stop + 1) div 2; if Med-Start>1 Then MergeSort(ADat, Start, Med - 1); if Stop-Med>0 Then MergeSort(ADat, Med, Stop); Rock1:=Start; Rock2:=Med; for Loop:=Start to Stop do if (Rock1=Med) or ((Rock2<=Stop) and (ADat[Rock1]>ADat[Rock2])) then Begin dd[Loop]:= ADat[Rock2]; inc(Rock2); End Else Begin dd[Loop]:= ADat[Rock1]; inc(Rock1); End; For Loop:=Start to Stop do ADat[Loop]:=dd[Loop]; End; Procedure ReadTest; Var L1, L2:Longint; Begin Max:=0; ReadLn( Count ); For L1:=1 to Count do Begin For L2:=L1 to Count do Begin Max:=Max+1; Read(Tab[L1][L2].Value); Tab[L1][L2].Pos:=L2; Total[Max]:=Tab[L1][L2].Value; End; ReadLn; // MergeSort(L1,L1,Count); End; MergeSort(Total,1,Max); End; Function Find(AStart, AFinish:Longint):Boolean; Var Loop:Longint; Sum:Int64; Begin Sum:=0; For Loop:=Count Downto 1 do Sum:=Sum+Total[Loop]; WriteLn(Sum); End; begin ReadTest; Find(1,Count); // ReadLn; end. { max 333373331310 }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | program Kug; {$mode objfpc}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} Classes { you can add units after this }; Type TDat=Record Value:Longint; Pos :Longint; End; TTab=Array[0..2001] of TDat; TTotal=Array[0..2000*2000] of Longint; var Tab:Array[0..2001] of TTab; D:TTab; DD,Total:TTotal; Max,Count:Longint; procedure MergeSort(Const Row,Start,Stop : integer);overload; var Med,Rock1,Rock2,Loop : integer; begin Med := (Start + Stop + 1) div 2; if Med-Start>1 Then MergeSort(Row, Start, Med - 1); if Stop-Med>0 Then MergeSort(Row, Med, Stop); Rock1:=Start; Rock2:=Med; for Loop:=Start to Stop do if (Rock1=Med) or ((Rock2<=Stop) and (Tab[Row][Rock1].Value>Tab[Row][Rock2].Value)) then Begin d[Loop]:= Tab[Row][Rock2]; inc(Rock2); End Else Begin d[Loop]:= Tab[Row][Rock1]; inc(Rock1); End; For Loop:=Start to Stop do Tab[Row][Loop]:=d[Loop]; End; procedure MergeSort(var ADat:TTotal;const Start,Stop : integer);overload; var Med,Rock1,Rock2,Loop : integer; begin Med := (Start + Stop + 1) div 2; if Med-Start>1 Then MergeSort(ADat, Start, Med - 1); if Stop-Med>0 Then MergeSort(ADat, Med, Stop); Rock1:=Start; Rock2:=Med; for Loop:=Start to Stop do if (Rock1=Med) or ((Rock2<=Stop) and (ADat[Rock1]>ADat[Rock2])) then Begin dd[Loop]:= ADat[Rock2]; inc(Rock2); End Else Begin dd[Loop]:= ADat[Rock1]; inc(Rock1); End; For Loop:=Start to Stop do ADat[Loop]:=dd[Loop]; End; Procedure ReadTest; Var L1, L2:Longint; Begin Max:=0; ReadLn( Count ); For L1:=1 to Count do Begin For L2:=L1 to Count do Begin Max:=Max+1; Read(Tab[L1][L2].Value); Tab[L1][L2].Pos:=L2; Total[Max]:=Tab[L1][L2].Value; End; ReadLn; // MergeSort(L1,L1,Count); End; MergeSort(Total,1,Max); End; Function Find(AStart, AFinish:Longint):Boolean; Var Loop:Longint; Sum:Int64; Begin Sum:=0; For Loop:=Count Downto 1 do Sum:=Sum+Total[Loop]; WriteLn(Sum); End; begin ReadTest; Find(1,Count); // ReadLn; end. { max 333373331310 } |