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
#include <stdio.h>
#include <string>
#include <math.h>
#include <map>
using namespace std;

int main(int argc, char const *argv[]) {
  int num, oneCount = 0, i, j;
  map<int, char> ascii = {
    { 3, 'a' },
    { 4, 'c' },
    { 5, 'g' },
    { 6, 'w' }
  };
  map<int, int> result = {
    { 3, 0 },
    { 4, 0 },
    { 5, 0 },
    { 6, 0 }
  };

  if (!scanf("%d", &num)) return 1;

  char digit;
  for (i = 0; i <= num*8; ++i) {
    if (!scanf("%c", &digit)) return 1;
    if (digit == '1') ++oneCount;
  }

  if (oneCount < num*3 || oneCount > num*6) {
    printf("NIE");
    return 0;
  }

  int tmp = oneCount - 3*num;
  result[4] = tmp / 6;
  result[5] = tmp / 6;
  result[6] = tmp / 6;

  int diff = tmp - (result[4] + 2*result[5] + 3*result[6]);
  switch (diff) {
    case 1:
      if (result[4] == 0) {
        result[4]+=1;
      } else {
        result[4]-=1; result[5]+=1;
      }
    break;
    case 2:
      if (result[4] == 0) {
        result[5]+=1;
      } else {
        result[4]-=1; result[6]+=1;
      }
    break;
    case 3: 
      if (result[4] == 0) {
        result[6]+=1;
      } else if (result[4] == 1) {
        result[4]-=1; result[6]+=1;
      } else {
        result[4]-=2; result[5]+=1; result[6]+=1;
      }
    break;
    case 4:
      if (result[4] == 0) {
        result[5]+=2;
      } else if (result[4] == 1) {
        result[4]-=1; result[5]+=1; result[6]+=1;
      } else {
        result[4]-=2; result[6]+=2;
      }
      break;
    case 5: 
      if (result[4] == 0) {
        result[5]+=1; result[6]+=1;
      } else if (result[4] == 1) {
        result[4]-=1; result[6]+=2;
      } else if (result[4] == 2) {
        result[4]-=2; result[5]+=1; result[6]+=1;
      } else {
        result[4]-=3; result[5]+=1; result[6]+=2;
      }
      break;
  }
  result[3] = num - result[4] - result[5] - result[6];

  for (i = 3; i <=6 ; ++i) {
    for (j = 0; j < result[i]; ++j) {
      printf("%c", ascii[i]);
    }
  }

  return 0;
}