about summary refs log tree commit diff
path: root/others/other/chonso1.pas
blob: 7e1040008a133c0f94f5d9fa175f2f3adb52a080 (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
var
  f: text;
  n, k, i, j: uint8;
  max, count: uint64;
  a: array[1..10, 1..10] of uint64;
  avail: array[1..10] of boolean = (true, true, true, true, true,
                                    true, true, true, true, true);


procedure chonso1(
  depth, len: uint8;
  sum: uint64
);

  var
    i: uint8;

  begin
    if len = k then
      begin
        if sum > max then
          begin
            max := sum;
            count := 1
          end
        else if sum = max then
          inc(count);
        exit
      end;

    for i := 1 to n do
      if avail[i] then
        begin
          avail[i] := false;
          chonso1(depth + 1, len + 1, sum + a[depth, i]);
          avail[i] := true
        end;

    if k - len <= n - depth then
      chonso1(depth + 1, len, sum);
  end;


begin
  assign(f, 'CHONSO1.INP');
  reset(f);
  readln(f, n, k);
  for i := 1 to n do
    for j := 1 to n do
      read(f, a[i, j]);
  close(f);

  max := 0;
  chonso1(1, 0, 0);

  assign(f, 'CHONSO1.OUT');
  rewrite(f);
  writeln(f, max, ' ', count);
  close(f)
end.