#include <iostream>
#include <vector>

using namespace std;

// Extracted helper function to verify if target_units can be produced
bool can_produce(long long target_units, int n, const vector<int>& composition, const vector<int>& stock, const vector<int>& cost, int budget) {
    long long total_cost = 0;
    
    for (int i = 0; i < n; ++i) {
        long long required_metal = target_units * composition[i];
        
        if (required_metal > stock[i]) {
            long long need_to_buy = required_metal - stock[i];
            total_cost += need_to_buy * cost[i];
        }
        
        // Early exit if the running cost exceeds the budget
        if (total_cost > budget) {
            return false;
        }
    }
    return true;
}

long long maxAlloys(int n, const vector<int>& composition, const vector<int>& stock, const vector<int>& cost, int budget) {
    // Upper bound set slightly higher than max budget + max stock based on constraints
    long long low = 0, high = 2e9; 
    long long max_units = 0;

    // Binary Search execution
    while (low <= high) {
        long long mid = low + (high - low) / 2;
        
        // Pass necessary arrays and variables to the outside function
        if (can_produce(mid, n, composition, stock, cost, budget)) {
            max_units = mid;   // mid is possible, store it
            low = mid + 1;     // try searching for a larger amount
        } else {
            high = mid - 1;    // mid is impossible, reduce target
        }
    }

    return max_units;
}

int main() {
    // Example test case
    int n = 2;
    vector<int> composition = {1, 2};
    vector<int> stock = {0, 1};
    vector<int> cost = {1, 1};
    int budget = 3;

    cout << "Maximum units of alloy: " << maxAlloys(n, composition, stock, cost, budget) << "\n";
    // Expected output: 1

    return 0;
}