Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var
- P,K,M,a,a1,a2 : array [1..100] of longint;
- i,n,x,kp,kk,km,j,x1,x2,sum1,sum2 : longint;
- s : string;
- begin
- readln(n,x);
- for i:=1 to n do
- read(a[i]);
- readln;
- readln(s);
- for i:=1 to length(s) do
- if pos('P',s[i])<>0 then begin
- kp:=kp+1;
- P[i]:=a[i];
- end;
- for i:=1 to length(s) do
- if pos('K',s[i])<>0 then begin
- kk:=kk+1;
- K[i]:=a[i];
- end;
- for i:=1 to length(s) do
- if pos('M',s[i])<>0 then begin
- km:=km+1;
- M[i]:=a[i];
- end;
- if kk=0 then begin
- writeln('NO');
- exit;
- end;
- if (km=0) and (kp=0) then begin
- writeln('NO');
- exit;
- end;
- for i:=1 to n-km-1 do
- begin
- a1[i]:=K[i];
- a1[i+1]:=P[i];
- end;
- for i:=1 to n-kp-1 do
- begin
- a2[i]:=K[i];
- a2[i+1]:=M[i];
- end;
- for i:=1 to n-km-1 do
- for j:=1 to n-km-i do
- if a1[j]<a1[j+1] then
- begin
- x1:=a1[j];
- a1[j]:=a1[j+1];
- a1[j+1]:=x1;
- end;
- for i:=1 to x do
- sum1:=sum1+a1[j];
- for i:=1 to n-kp-1 do
- for j:=1 to n-kp-i do
- if a2[j]<a2[j+1] then
- begin
- x2:=a2[j];
- a2[j]:=a2[j+1];
- a2[j+1]:=x2;
- end;
- for i:=1 to x do
- sum2:=sum2+a[j];
- if sum1>sum2 then writeln(sum1)
- else writeln(sum2);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement