⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ac1125.pas

📁 同济大学 Online在线题库 AC源代码合集 程序设计竞赛必看资料
💻 PAS
字号:
program tju1125;
const
  maxn=100;
var
  a:array[1..maxn]of longint;
  p:array[1..maxn]of byte;
  n,i,j:byte;
  s,m:longint;
function min(a,b:longint):longint;
  begin
    if a<b then min:=a else min:=b;
  end;
procedure go(x:byte);
  var
    c,t,y:longint;
  begin
    c:=0;t:=maxlongint;
    repeat
      inc(c);
      if a[x]<t then t:=a[x];
      y:=p[x];p[x]:=0;x:=y;
    until p[x]=0;
    inc(s,min((c-2)*t,t+(c+1)*m));
  end;
begin
  repeat
    read(n);
    if n=0 then halt;
    s:=0;m:=maxlongint;
    for i:=1 to n do begin
      read(a[i]);
      inc(s,a[i]);
      if a[i]<m then m:=a[i];
    end;
    fillchar(p,sizeof(p),1);
    for i:=2 to n do
      for j:=1 to i-1 do
        if a[i]>a[j] then inc(p[i]) else inc(p[j]);

    for i:=1 to n do
      if p[i]>0 then
        go(i);
    writeln(s);
  until false;
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -