SEQUENCE.PAS 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. {$A+,B-,D+,E+,F-,G-,I+,L+,N-,O-,P-,Q-,R-,S+,T-,V+,X+,Y+}
  2. {$M 64000,0,655360}
  3. program Sequence;
  4. const
  5. fin = 'sequence.in';
  6. fon = 'sequence.out';
  7. maxN = 1000;
  8. var
  9. N : integer;
  10. A : array[1..2,0..2]of integer;
  11. D : array[0..maxN] of integer;
  12. V : array[0..maxN]of integer;
  13. E : array[0..maxN,1..6,0..1]of integer;
  14. i,j,k,l : integer;
  15. procedure Input;
  16. var f:text;
  17. begin
  18. assign(f,fin); reset(f);
  19. read(f,n);
  20. for i:=1 to 2 do
  21. read(f,a[i,1],a[i,2],a[i,0]);
  22. j:=a[1,1];
  23. a[1,1]:=a[1,0]-a[1,2];
  24. a[1,2]:=a[1,0]-j;
  25. close(f);
  26. end;
  27. procedure Construct;
  28. begin
  29. fillchar(v,sizeof(v),0);
  30. for i:=1 to n do
  31. begin
  32. inc(v[i-1]); inc(v[i]);
  33. e[i-1,v[i-1],0]:=i;
  34. e[i-1,v[i-1],1]:=0;
  35. e[i,v[i],0]:=i-1;
  36. e[i,v[i],1]:=-1;
  37. end;
  38. for i:=1 to 2 do
  39. for j:=0 to n-a[i,0] do
  40. begin
  41. k:=j+a[i,0];
  42. inc(v[j]); inc(v[k]);
  43. e[j,v[j],0]:=k;
  44. e[j,v[j],1]:=a[i,1];
  45. e[k,v[k],0]:=j;
  46. e[k,v[k],1]:=-a[i,2];
  47. end;
  48. end;
  49. procedure Done;
  50. var f:text;
  51. begin
  52. assign(f,fon); rewrite(f);
  53. if l > n
  54. then write(f,-1) else
  55. for i:=1 to n do
  56. write(f,d[i]-d[i-1]);
  57. writeln(f); close(f);
  58. end;
  59. begin
  60. Input;
  61. Construct;
  62. fillchar(d,sizeof(d),0);
  63. l:=0;
  64. repeat
  65. inc(l); k:=0;
  66. for i:=0 to n do
  67. for j:=1 to v[i] do
  68. if d[i]+e[i,j,1] > d[e[i,j,0]] then
  69. begin
  70. d[e[i,j,0]]:=d[i]+e[i,j,1];
  71. k:=1;
  72. end;
  73. until (k=0) or (l>n);
  74. Done;
  75. end.