Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.34 KB | None | 0 0
  1. var
  2.   P,K,M,a,a1,a2 : array [1..100] of longint;
  3.   i,n,x,kp,kk,km,j,x1,x2,sum1,sum2 : longint;
  4.   s : string;
  5. begin
  6.   readln(n,x);
  7.   for i:=1 to n do
  8.     read(a[i]);
  9.   readln;
  10.   readln(s);
  11.   for i:=1 to length(s) do
  12.     if pos('P',s[i])<>0 then begin
  13.       kp:=kp+1;
  14.       P[i]:=a[i];
  15.     end;
  16.   for i:=1 to length(s) do
  17.     if pos('K',s[i])<>0 then begin
  18.       kk:=kk+1;
  19.       K[i]:=a[i];
  20.     end;
  21.   for i:=1 to length(s) do
  22.     if pos('M',s[i])<>0 then begin
  23.       km:=km+1;
  24.       M[i]:=a[i];
  25.     end;
  26.   if kk=0 then begin
  27.     writeln('NO');
  28.     exit;
  29.   end;
  30.   if (km=0) and (kp=0) then begin
  31.     writeln('NO');
  32.     exit;
  33.   end;
  34.   for i:=1 to n-km-1 do
  35.     begin
  36.       a1[i]:=K[i];
  37.       a1[i+1]:=P[i];
  38.     end;
  39.   for i:=1 to n-kp-1 do
  40.     begin
  41.       a2[i]:=K[i];
  42.       a2[i+1]:=M[i];
  43.     end;
  44.   for i:=1 to n-km-1 do
  45.     for j:=1 to n-km-i do
  46.       if a1[j]<a1[j+1] then
  47.         begin
  48.           x1:=a1[j];
  49.           a1[j]:=a1[j+1];
  50.           a1[j+1]:=x1;
  51.         end;
  52.   for i:=1 to x do
  53.     sum1:=sum1+a1[j];
  54.   for i:=1 to n-kp-1 do
  55.     for j:=1 to n-kp-i do
  56.       if a2[j]<a2[j+1] then
  57.         begin
  58.           x2:=a2[j];
  59.           a2[j]:=a2[j+1];
  60.           a2[j+1]:=x2;
  61.         end;
  62.   for i:=1 to x do
  63.     sum2:=sum2+a[j];
  64.   if sum1>sum2 then writeln(sum1)
  65.   else writeln(sum2);
  66. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement