program zraz;

{VLADIMIR BRANKOV - YUGOSLAVIA}

uses crt;

var f,g:text;
    c:char;
    p,q:integer;

function ucitaj:integer; forward;

function prost:integer;
var z:integer;
begin
  if not eoln(f)
    then
      begin
        if c='('
          then
            begin
              read(f,c);
              z:=ucitaj;
              if not eoln(f) then read(f,c);
            end
          else
            begin
              while (c<>'+') and (c<>'*') and (not eoln(f)) do read(f,c);
              z:=0;
            end
      end
    else z:=0;
  prost:=z
end;

function izraz:integer;
var z,z2:integer;
begin
  z:=prost;
  while (not eoln(f)) and (c='*') do
    begin
      read(f,c);
      z2:=prost;
      if z2>z then z:=z2;
      z:=z+q
    end;
  izraz:=z
end;

function ucitaj;
var z,z2:integer;
begin
  z:=izraz;
  while (not eoln(f)) and (c='+') do
    begin
      read(f,c);
      z2:=izraz;
      if z2>z then z:=z2;
      z:=z+p
    end;
  ucitaj:=z
end;

begin
  clrscr;
  assign(f,'input1.txt');
  assign(g,'output.txt');
  reset(f);
  rewrite(g);
  readln(f,p,q);

  while not eof(f) do
    begin
      read(f,c);
      writeln(G,ucitaj);
      readln(f)
    end;

  close(f);
  close(g);
  writeln('The solutions are in output.txt.');
  writeln;
  writeln('Press ENTER to exit...');
  readln
end.
