fork download
  1. program bouquet;
  2. type elenco = array [0..100000] of Longint;
  3. var N,J,i : Longint;
  4. F : elenco;
  5.  
  6. Procedure scambia (var a,b: longint);
  7. var x:longint;
  8. begin
  9. x:=a;
  10. a:=b;
  11. b:=x;
  12. end;
  13. Procedure ordinamento (estremoi,estremos: longint; var v : elenco; ordinato:boolean);
  14. var inf, sup, medio:longint;
  15. pivot :longint;
  16. begin
  17. inf:=estremoi;
  18. sup:=estremos;
  19. medio:= (estremoi+estremos) div 2;
  20. pivot:=v[medio];
  21. repeat
  22. if (ordinato) then
  23. begin
  24. while (v[inf]<pivot) do inf:=inf+1;
  25. while (v[sup]>pivot) do sup:=sup-1;
  26. end;
  27. if inf<=sup then
  28. begin
  29. scambia(v[inf],v[sup]);
  30. inf:=inf+1;
  31. sup:=sup-1;
  32. end;
  33. until inf>sup;
  34. if (estremoi<sup) then ordinamento(estremoi,sup,v,ordinato);
  35. if (inf<estremos) then ordinamento(inf,estremos,v,ordinato);
  36. end;
  37.  
  38. function componi (N:Longint; J:Longint; F: array of Longint) : longint;
  39. var minimo, massimo,i, somma, ultimo : Longint;
  40. uguali : boolean;
  41. diff : array [0..100000] of Longint;
  42. begin
  43. massimo:=F[N-1]; minimo:=F[0]; uguali:=true;
  44. for i:=1 to N-1 do if F[i]<>F[0] then uguali:=false;
  45. somma:=MAXLONGINT;
  46. if J=0 then componi:=minimo
  47. else
  48. begin
  49. if uguali=true then componi:=minimo + J div N
  50. else
  51. begin
  52. ultimo:=N-1;
  53. while somma>J do
  54. begin
  55. somma:=0;
  56. for i:=0 to N-1 do begin diff[i]:=massimo-F[i]; somma:=somma+diff[i]; end;
  57. massimo:=massimo-1;
  58. end;
  59. if somma<=J then componi:=(massimo+1)+(J-somma) div N;
  60. end;
  61. end;
  62. end;
  63.  
  64.  
  65. begin
  66. readln(N,J);
  67. for i:=0 to N-1 do read(F[i]); readln;
  68. ordinamento(0,N-1,F,true);
  69. writeln(componi(N,J,F));
  70. end.
Success #stdin #stdout 0.01s 5288KB
stdin
4 7
2 6 4 3

stdout
5