<|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 cost(x) ≤ N.  
  • Because cost(x) grows monotonically, we can either:  
    a) Binary-search x in [0, some upper bound].  
    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.

3. Full Solution Approach  
  1. If N < p1, return 0 immediately.  
  2. Subtract p1 from N: these funds cover the first k1 seconds. Set answer = k1.  
  3. From the remaining balance, buy up to k2 seconds at p2 per second:  
     take2 = min(k2, N // p2)  
     answer += take2  
     N -= take2·p2  
  4. Finally, use all leftover balance for p3-priced seconds:  
     take3 = N // p3  
     answer += take3  
  5. Return answer.  
  This runs in O(1) time and uses O(1) memory, easily within the problem limits.

4. C++ Implementation with Detailed Comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

// We use 64-bit integers in case N and costs are large.
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    long long N;    // Initial balance in cents
    long long k1, k2; // Durations of the first two billing blocks
    long long p1, p2, p3; // Costs: flat fee and per-second costs

    // Read input: N k1 k2 p1 p2 p3
    cin >> N >> k1 >> k2 >> p1 >> p2 >> p3;

    // 1) If we cannot pay the initial flat fee, we can't talk at all.
    if (N < p1) {
        cout << 0 << "\n";
        return 0;
    }

    // 2) Pay the flat fee p1 to cover up to k1 seconds.
    N -= p1;
    long long answer = k1;

    // 3) Buy up to k2 seconds at p2 per second.
    //    We can afford at most N/p2 seconds, but also no more than k2.
    long long take2 = min(k2, N / p2);
    answer += take2;
    N -= take2 * p2;

    // 4) With remaining balance, buy as many p3-seconds as possible.
    long long take3 = N / p3;
    answer += take3;

    // 5) Output the total seconds.
    cout << answer << "\n";
    return 0;
}
```

5. Python Implementation with Detailed Comments  
```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:  
We first ensure Kate can pay the initial connection fee p1. That covers up to k1 seconds regardless of how small k1 is. Then we treat the next k2 seconds as a separate “block” priced at p2 per second, buying as many of those as the remaining balance allows (but no more than k2). Finally, any leftover money is spent on additional seconds at p3 per second. This greedy block-by-block purchase works because each block’s price is fixed per second and the total cost function is monotonically increasing in time.