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
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <bits/stdc++.h>
#include <unordered_map>


using namespace std;
#define LL long long
#define BIGMOD 1000000007LL

#define DBG(X) 
#define DBG_BIG(X) 

int G_CAPACITY[3];

struct State {
    int level[3];
    State(int L[3]) 
    {
        level[0] = L[0];
        level[1] = L[1];
        level[2] = L[2];
    }

    State przelej(int from, int to)
    {
        State res = *this;
        int free = G_CAPACITY[to] - level[to];
        int possible = min(res.level[from], free);
        res.level[from] -= possible;
        res.level[to] += possible;
        return res;
    }

    vector<State> next()
    {
        vector<State> res;
        for (int i=0; i < 3; i++)
        {
          for (int j=0; j < 3; j++)
          {
              if (i==j) continue;
              res.push_back(przelej(i,j));
          }
        }
        return res;
    }
    
    void print() {
       printf("(%d,%d,%d)\n",level[0], level[1], level[2]);
    }

    struct HashFunction
    {
        size_t operator()(const State& s) const
        {
            size_t hash = 0;
            for (int i=0; i < 3; i++)
            {
                hash += s.level[i];
                hash *= 100333;
            }
            return hash;
        }
    };

    bool operator==(const State &other) const {
      
       for (int i=0; i < 3; i++)
       {
           if (level[i] != other.level[i]) return false;
       }
       return true;
    }

};

vector<int> solve(int capacity[3], int level[3])
{
    vector<int> answer(capacity[2]+1, -1);
    for (int i=0; i < 3; i++)
    {
        G_CAPACITY[i] = capacity[i];
    }
    queue<pair<State, int> > Q;
    Q.push(make_pair(State(level), 0));
    
    unordered_map<State, bool, State::HashFunction> seen;
    
    while (!Q.empty())
    {
        pair<State, int> elem = Q.front(); Q.pop();
        State s = elem.first;
        int steps = elem.second;
        DBG(s.print());
        if (seen.find(s) != seen.end()) continue;
        seen[s] = true;

        DBG(printf("seen[s]=%d\n", seen[s]));
        for (int i=0; i < 3; i++)
        {
            if (answer[s.level[i]] == -1)
            {
                answer[s.level[i]] = steps;
            }
        }
        vector<State> nextStates = s.next();
        for (State &ns : nextStates) 
        {
            if (seen.find(ns) == seen.end())
            {
                Q.push(make_pair(ns, steps+1));
            }
        }
    }
    return answer;
}

int main() {

    int A,B,C,a,b,c;
    scanf("%d%d%d", &A,&B,&C);
    scanf("%d%d%d", &a,&b,&c);
    
    int capacity[3] = {A,B,C};
    int level[3] = {a,b,c};
    vector<int> answer = solve(capacity, level);
    for (int i=0; i < answer.size(); i++)
    {
        printf("%d ", answer[i]);
    }
    printf("\n");
    return 0;
}