about summary refs log tree commit diff
path: root/others/mkcal/llgcmm.pas
blob: 76ea699681136f8053207fa88ef97484c0df3dca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
type
  foo_t = record
            t, idx: integer
          end;

var
  f: text;
  n, m, i, j, k: integer;
  a: array of foo_t;
  b: array of array of foo_t;
  c: array of longint;
  foo: foo_t;
  tmp: longint;


procedure swp(var x, y: foo_t);
  var
    tmp: integer;

  begin
    tmp := x.t;
    x.t := y.t;
    y.t := tmp;

    tmp := x.idx;
    x.idx := y.idx;
    y.idx := tmp
  end;


begin
  assign(f, 'LLGCMM.INP');
  reset(f);

  readln(f, n, m);
  setlength(a, n);

  for i := 0 to n - 1 do
    read(f, a[i].t);

  close(f);

  for i := 1 to n do
    a[i - 1].idx := i;

  for i := 0 to n - 2 do
    for j := i to n - 1 do
      if a[i].t < a[j].t then
        swp(a[i], a[j]);

  setlength(b, m);
  setlength(c, m);

  for i := 0 to m - 1 do
    begin
      setlength(b[i], 0);
      c[i] := 0
    end;

  for i := 0 to n - 1 do
    begin
      tmp := c[0];
      k := 0;

      for j := 1 to m - 1 do
        if c[j] < tmp then
          begin
            k := j;
            tmp := c[j]
          end;

      setlength(b[k], length(b[k]) + 1);
      b[k][length(b[k]) - 1].t := a[i].t;
      b[k][length(b[k]) - 1].idx := a[i].idx;
      c[k] := c[k] + a[i].t
    end;

  tmp := 0;
  for i := 0 to m - 1 do
    if c[i] > tmp then
      tmp := c[i];

  assign(f, 'LLGCMM.OUT');
  rewrite(f);

  writeln(f, tmp);

  for i := 0 to m - 1 do
    begin
      for foo in b[i] do
        write(f, foo.idx, ' ');

      writeln(f);
    end;

  close(f)
end.