<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

353. Billing
Time limit per test: 0.25 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard



A girl named Kate has a contract with the Berland only cell phone operator BTS. Her tariff plan is called "Very Profitable" and all Kate can do is to make phone calls. All incoming calls are free. The outgoing calls are billed in the following way: at the very beginning of the call Kate's account is charged p1 cents for the first k1 seconds of the call. Each of the next k2 seconds costs p2 cents. And all remaining time costs p3 cents per second. The account is always billed instantly at the end of each second. If the account balance becomes non-positive, the call is automatically dropped.

Kate has put N cents to her account and she is going to talk with her very good friend. Kate would like to know what is the maximum number of seconds she can talk during a single call.

If N < p1, Kate does not have enough money to establish the call.

Input
The input contains 6 integer numbers N, k1, k2, p1, p2, p3, (; ; ).

Output
Write to the output just one integer number — the maximum number of seconds Kate can talk.

Example(s)
sample input
sample output
20 3 3 3 4 2
9

sample input
sample output
3 15 3 5 2 3
0

sample input
sample output
1 1 1 1 3 1
1

sample input
sample output
2 1 1 1 3 1
2

<|response|>
1. Abridged Problem Statement
Kate has N cents in her phone account. Outgoing calls are billed as follows:
  – As soon as the call starts, she pays p1 cents for up to k1 seconds (flat fee).
  – The next up to k2 seconds cost p2 cents per second.
  – Any further seconds cost p3 cents per second.
Charges are deducted at the end of each second, and if her balance becomes non-positive, the call drops immediately. Incoming calls are free. Compute the maximum whole seconds Kate can talk in one call. If N < p1, she cannot even establish a call, so the answer is 0.

2. Key Observations
  • The total cost as a function of talk time x is non-decreasing and piecewise linear:
    – For 1 ≤ x ≤ k1: cost = p1.
    – For k1+1 ≤ x ≤ k1+k2: cost = p1 + (x−k1)·p2.
    – For x > k1+k2: cost = p1 + k2·p2 + (x−k1−k2)·p3.
  • We need the largest x such that the call survives x seconds.
  • Because cost(x) grows monotonically, we can either:
    a) Binary-search x in [0, some upper bound], evaluating cost(x) in O(1).
    b) Compute directly in O(1) by “buying” blocks of time greedily: pay p1, then buy as many p2-seconds as possible (up to k2), then use leftover funds for p3-seconds.
  • Subtlety: the balance is only checked at the end of each second and the call drops when it becomes non-positive. So a second is affordable while cost(x) ≤ N; if the best x found by binary search has cost strictly below N, one more second can still be started (the balance only becomes non-positive at the end of it), so we add one in that boundary case.

3. Full Solution Approach
  We use the binary-search formulation. Define eval(x) = p1 + min(max(0,x−k1),k2)·p2 + max(0,x−k1−k2)·p3, the total cost of talking x seconds (for x ≥ 1).
  1. Binary search over x in [0, LIMIT] for the largest x with eval(x) ≤ N. This naturally yields 0 when N < p1, since even x = 1 costs p1.
  2. After the search, if eval(ans) < N and ans ≠ 0, increment ans to handle the end-of-second boundary case.
  3. Output ans.
  This runs in O(log LIMIT) time and O(1) memory, easily within the problem limits.

4. C++ Implementation
```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;
};

int64_t N, k1, k2, p1, p2, p3;

void read() {
    cin >> N >> k1 >> k2 >> p1 >> p2 >> p3;
}

int64_t eval(int64_t x) {
    int64_t ans = p1;
    if(x > k1) {
        ans += min(x - k1, k2) * p2;
    }

    if(x > k1 + k2) {
        ans += (x - k1 - k2) * p3;
    }

    return ans;
}

void solve() {
    // eval(x) returns the total cost of talking x seconds: a flat p1 for the
    // first k1 seconds, then p2 per second for the next k2, then p3 per second
    // afterwards. The cost is monotone non-decreasing in x, so we binary
    // search for the largest x whose cost is still <= N (the cents available).
    //
    // The starting cost is p1, so if N < p1 the loop yields ans = 0 (no call
    // possible). The final adjustment bumps ans by one to cover the boundary
    // case where the next second can still be afforded because the balance
    // only becomes non-positive at the very end of that second.

    int64_t low = 0, high = (int)3e6 + 42, mid, ans = 0;
    while(low <= high) {
        mid = (low + high) / 2;
        if(eval(mid) <= N) {
            ans = mid;
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }

    if(eval(ans) < N && ans != 0) {
        ans++;
    }

    cout << ans << '\n';
}

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;
}
```

5. Python Implementation
```python
import sys

def max_talk_time(N, k1, k2, p1, p2, p3):
    # 1) If not enough to pay p1, return 0.
    if N < p1:
        return 0

    # 2) Pay flat fee p1 for the first k1 seconds.
    N -= p1
    total_seconds = k1

    # 3) Next k2 seconds cost p2 each.
    #    We can buy at most k2, and at most N//p2 with remaining balance.
    take2 = min(k2, N // p2)
    total_seconds += take2
    N -= take2 * p2

    # 4) Remaining seconds cost p3 each.
    take3 = N // p3
    total_seconds += take3

    return total_seconds

def main():
    data = sys.stdin.read().strip().split()
    N, k1, k2, p1, p2, p3 = map(int, data)
    print(max_talk_time(N, k1, k2, p1, p2, p3))

if __name__ == "__main__":
    main()
```

Explanation Recap:
The greedy Python version buys time block by block: first ensure Kate can pay the connection fee p1 (covering up to k1 seconds), then buy as many of the next k2 seconds at p2 each as the balance allows, then spend the leftover on p3-priced seconds. The C++ version reaches the same answer by binary searching the largest x whose total cost stays within N, with a +1 adjustment for the end-of-second boundary. Both work because the cost function is monotonically increasing in time.
