p478.ans1
======================
1 2
2 1

=================
p478.in1
======================
3 3
2
2 3

=================
p478.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int a, b, n;
vector<int> cnt;

void read() {
    cin >> a >> b;
    cin >> n;
    cnt.resize(n);
    cin >> cnt;
    cnt.insert(cnt.begin(), b);
}

void dfs(
    int pos, int cnt_boys, vector<vector<bool>>& visited,
    vector<vector<tuple<int, int, int>>>& par
) {

    int cnt_girls = cnt[pos] - cnt_boys;
    
    if(cnt_girls < 0 || cnt_boys < 0 || cnt_boys > a || cnt_girls > b) {
        return;
    }

    if(pos == n) {
        return;
    }

    for(int cnt_girls_leave = 0; cnt_girls_leave <= cnt_girls;
        cnt_girls_leave++) {
        int new_cnt_girls = cnt_girls - cnt_girls_leave;
        int new_cnt_boys = cnt[pos + 1] - new_cnt_girls;
        if(new_cnt_boys >= cnt_boys && new_cnt_boys <= a &&
           !visited[pos + 1][new_cnt_boys]) {
            par[pos + 1][new_cnt_boys] = {
                cnt_boys, new_cnt_boys - cnt_boys, cnt_girls_leave
            };
            visited[pos + 1][new_cnt_boys] = true;
            dfs(pos + 1, new_cnt_boys, visited, par);
        }
    }
}

void solve() {
    vector<vector<bool>> visited(n + 1, vector<bool>(a + 1, false));
    vector<vector<tuple<int, int, int>>> par(
        n + 1, vector<tuple<int, int, int>>(a + 1, {-1, -1, -1})
    );

    dfs(0, 0, visited, par);

    for(int cnt_boys = 0; cnt_boys <= a; cnt_boys++) {
        if(visited[n][cnt_boys]) {
            vector<pair<int, int>> ans;
            int pos = n, c = cnt_boys;
            while(pos > 0) {
                auto [prev_boys, move_boys, move_girls] = par[pos][c];
                ans.push_back({move_boys, move_girls});
                c = prev_boys;
                pos--;
            }

            reverse(ans.begin(), ans.end());
            for(auto [x, y]: ans) {
                cout << x << ' ' << y << '\n';
            }
            return;
        }
    }
    cout << "ERROR" << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
p478.ans2
======================
ERROR

=================
p478.ans3
======================
0 0

=================
p478.in2
======================
3 3
3
1 2 5

=================
p478.in3
======================
2 2
1
2

=================
statement.txt
======================
478. Excursion
Time limit per test: 0.25 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard



One day a group of students, consisting of boys (a heads) and girls (b heads), got to an idea to make an excursion led by their school teacher over the city, they lived in. At the very start of the party schoolboys went away walking separately from the teacher, rather than obedient school girls, who stayed with the teacher. Anyhow, when approaching any boutique during the excursion, some girls can leave the group to go for a fitting their lovely dresses, and they will never come back again. On the contrary, ruddy cheeked and cheered up boys can go back to the teacher and stay with him with great enjoy until the end of excursion. At some points of time scrupulous teacher recalculates the number of students in the group and writes it down in his notebook.

Now the teacher wants to evaluate the effectiveness of extracurricular work, so he askes you to find any feasible student joining/disjoining schedule.

Input
The first line of input contains two integers a and b (1 ≤ a, b ≤ 100) — the number of boys and girls respectively. The second line has the only integer n (1 ≤ n ≤ 100) — the number of notes in teacher's book. The third one contains exactly n non-negative integers not exceeding 200, denoting numbers the teacher wrote down, in the order of their appearance in the notebook.

Output
If there are any mistakes in teacher's notes, that is no feasible schedule exists, print "ERROR". Otherwise, print to output n lines. i-th line should contain two non-negative integers, denoting the number of schoolboys joined and the number of schoolgirls separated from the teacher respectively exactly before he recalculated students at i-th time. If there are multiple solutions, output any.

Example(s)
sample input
sample output
3 3
2
2 3
1 2
2 1

sample input
sample output
3 3
3
1 2 5
ERROR

sample input
sample output
2 2
1
2
0 0

=================
