Rock, Paper, Scissors
Contest: Codeforces Round 674 (Div. 3)
Difficulty rating: 1800
Tags: brute force, constructive algorithms, flows, greedy, math

Alice and Bob have decided to play the game "Rock, Paper, Scissors".

The game consists of several rounds, each round is independent of each other. In each round, both players show one of the following things at the same time: rock, paper or scissors. If both players showed the same things then the round outcome is a draw. Otherwise, the following rules applied:

- if one player showed rock and the other one showed scissors, then the player who showed rock is considered the winner and the other one is considered the loser;
- if one player showed scissors and the other one showed paper, then the player who showed scissors is considered the winner and the other one is considered the loser;
- if one player showed paper and the other one showed rock, then the player who showed paper is considered the winner and the other one is considered the loser.

Alice and Bob decided to play exactly $$$n$$$ rounds of the game described above. Alice decided to show rock $$$a_1$$$ times, show scissors $$$a_2$$$ times and show paper $$$a_3$$$ times. Bob decided to show rock $$$b_1$$$ times, show scissors $$$b_2$$$ times and show paper $$$b_3$$$ times. Though, both Alice and Bob did not choose the sequence in which they show things. It is guaranteed that $$$a_1 + a_2 + a_3 = n$$$ and $$$b_1 + b_2 + b_3 = n$$$.

Your task is to find two numbers:

1. the minimum number of round Alice can win;
2. the maximum number of rounds Alice can win.

Input format:
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 10^{9}$$$) — the number of rounds.

The second line of the input contains three integers $$$a_1, a_2, a_3$$$ ($$$0 \le a_i \le n$$$) — the number of times Alice will show rock, scissors and paper, respectively. It is guaranteed that $$$a_1 + a_2 + a_3 = n$$$.

The third line of the input contains three integers $$$b_1, b_2, b_3$$$ ($$$0 \le b_j \le n$$$) — the number of times Bob will show rock, scissors and paper, respectively. It is guaranteed that $$$b_1 + b_2 + b_3 = n$$$.

Output format:
Print two integers: the minimum and the maximum number of rounds Alice can win.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first example, Alice will not win any rounds if she shows scissors and then paper and Bob shows rock and then scissors. In the best outcome, Alice will win one round if she shows paper and then scissors, and Bob shows rock and then scissors.

In the second example, Alice will not win any rounds if Bob shows the same things as Alice each round.

In the third example, Alice always shows paper and Bob always shows rock so Alice will win all three rounds anyway.

Example input:
2
0 1 1
1 1 0

Example output:
0 1

Example input:
15
5 5 5
5 5 5

Example output:
0 15

Editorial:
The maximum number of rounds Alice can win is pretty easy to calculate greedily: $$$min(a_1, b_2) + min(a_2, b_3) + min(a_3, b_1)$$$. What about the minimum number of rounds?
It can be shown that if we started using some combination we are better to end it before using the other one. There are six possible combinations to not win the round:
• $$$a_1$$$ and $$$b_1$$$.
• $$$a_2$$$ and $$$b_2$$$.
• $$$a_3$$$ and $$$b_3$$$.
• $$$a_1$$$ and $$$b_3$$$.
• $$$a_2$$$ and $$$b_1$$$.
• $$$a_3$$$ and $$$b_2$$$.
We can iterate over all permutations of these combinations (there are $$$6! = 720$$$ possible permutations) and greedily apply them. Use the first while it is possible, then the second, and so on, and find the best answer.
It is also possible that the order of these combinations does not matter, but we didn't prove that fact.
Time complexity: $$$O(1)$$$.