fork(2) download
  1. uses math;
  2. Var i,j,t,first,n:longint;
  3. used:array[0..1111]of boolean;
  4. a:array[0..1111]of longint;
  5. tmp,ans:int64;
  6. procedure go(v,dest:longint;time:int64);
  7. var d,vv:longint;
  8. begin
  9. vv:=v;
  10. if v<dest then d:=1
  11. else d:=-1;
  12. while v<>dest do
  13. begin
  14. v:=v+d;
  15. if used[v] then continue;
  16. tmp:=tmp+abs(a[vv]-a[v])+time;
  17. used[v]:=true;
  18. end;
  19. end;
  20. begin
  21. // assign(input,'cowrun.in');
  22. // reset(input);
  23. // assign(output,'cowrun.out');
  24. // rewrite(output);
  25. read(n);
  26. for i:=1 to n do
  27. read(a[i]);
  28. inc(n);
  29. a[n]:=0;
  30. for i:=1 to n do
  31. for j:=i+1 to n do
  32. if a[i]>a[j] then
  33. begin
  34. a[0]:=a[i];
  35. a[i]:=a[j];
  36. a[j]:=a[0];
  37. end;
  38. for i:=1 to n do
  39. if a[i]=0 then begin first:=i;break;end;
  40. ans:=maxlongint*maxlongint;
  41. for i:=1 to n do
  42. begin
  43. fillchar(used,sizeof(used),false);
  44. tmp:=0;
  45. used[first]:=true;
  46. go(first,i,0);
  47. go(i,n,abs(a[i]));
  48. go(n,1,abs(a[i])+abs(a[i]-a[n]));
  49. ans:=min(ans,tmp);
  50. fillchar(used,sizeof(used),false);
  51. tmp:=0;
  52. used[first]:=true;
  53. go(first,i,0);
  54. go(i,1,abs(a[i]));
  55. go(1,n,abs(a[i])+abs(a[i]-a[1]));
  56. ans:=min(ans,tmp);
  57. end;
  58. Writeln(ans);
  59. close(input);
  60. close(output);
  61. end.
Success #stdin #stdout 0s 408KB
stdin
6 
1 -1 10 -10 100 -100
stdout
502