We are given: n clients, each wants x_i low-quality and y_i high-quality photos. Camera memory: d MB. Each low-quality photo takes a MB, each high-quality takes b MB. To serve a client, we must allocate memory for all photos requested by that client. We start with empty memory, and we never delete photos. So the memory used is cumulative sum of a * (sum of x_i of served clients) + b * (sum of y_i of served clients). This total must not exceed d.
We need to choose a subset of clients (preserving order? Actually not necessarily order. The problem says "print the numbers of the client in any order." It doesn't require serving in input order; we can serve any subset. But note: "Valera also does not delete photos from the camera so that the camera's memory gradually fills up." However, there is no constraint that clients must be served in a specific order (like they come sequentially). Actually, the description: "Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve." It might imply that clients come in some order, but we can choose which to serve. Usually in such problems, the order of serving is arbitrary because we can rearrange clients? But careful: The statement: "Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data." So we just need to output any subset. There's no requirement about serving order.
So the problem reduces to: Given items each with cost = ax_i + by_i. We have capacity d. We want to maximize number of items (clients) we can select, total cost ≤ d. That's classic knapsack with all items weight = cost and value = 1, maximize count. But n up to 1e5, d up to 1e9, costs up to (ax_i + by_i) where x_i,y_i up to 1e5, a,b up to 1e4, so cost up to (1e41e5 + 1e41e5) = 2e9, which can exceed d. But d up to 1e9, so many clients may not fit individually. However, we want maximize count, not minimize total cost. This is like we want to pick as many items as possible with total weight ≤ d. The classic greedy for maximizing count given weight limit is to choose items with smallest weight first. Indeed, to maximize number of items under a total weight constraint, you should take the lightest items. Because any optimal solution can be transformed: if you have a heavier item, you could replace it with a lighter one not in the set to get same or more count (but careful: number of items fixed, you want to minimize total weight for a given count; so to maximize count, you want to pick smallest weights). So the optimal strategy: sort clients by their cost (ax_i + by_i) ascending, then take as many as possible until cumulative sum exceeds d. That yields maximum number of clients, since any set of k clients must have total weight at least sum of k smallest weights; if sum of k smallest ≤ d, then k is achievable; else not. So maximum k is the largest such that prefix sum of sorted costs ≤ d.
Thus solution: compute cost_i = ax_i + by_i. Sort indices by cost_i ascending. Compute prefix sums. Find max prefix length L where sum ≤ d. Output L and the indices (original numbers) of those L clients (in any order). That yields maximum number.
But is there any nuance? Check examples:
Example1:
n=3, d=10, a=2, b=3
Clients:
1: x1=1,y1=4 => cost = 21+34 = 2+12=14 >10 (can't serve alone)
2: x2=2,y2=1 => cost = 22+31 = 4+3=7
3: x3=1,y3=0 => cost = 21+30 = 2
Sort: costs: 2 (client3), 7 (client2), 14 (client1). Prefix sums: 2 ≤10; 2+7=9 ≤10; 9+14=23 >10. So max L=2. Output clients 3 and 2 (any order). matches.
Example2:
n=3, d=6, a=6,b=6
Clients:
1:1,1 => cost=61+61=12 >6
2:1,0 => cost=6
3:1,0 => cost=6
Sort: costs:6(client2),6(client3),12(client1). Prefix sums:6 ≤6; 12>6 => max L=1. Output any one of the clients with cost 6? The sample output is 1 and client 2. That's fine. Could also be 3.
Thus matches.
Now, is there any catch? The problem mentions "To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space." That's just the per-photo memory requirement; but when making multiple photos for a client, we need to ensure that at each photo we have enough memory. However, since we allocate all photos for a client at once? The memory usage increases stepwise per photo, but if we have enough total memory for the whole set, is it possible that we might run out during the process because of ordering? For example, if we need to allocate a sequence of photos, each requiring that at the moment of taking, there is enough free memory. Since we never delete, the cumulative sum used at any point is sum of sizes of all taken photos so far. For a client with multiple low and high quality photos, if we intermix them, the order of taking might matter? Actually, each photo individually consumes its memory when taken. So if we need to take x low and y high photos for a client, the total memory needed at the end is ax + by, but along the way, the memory used grows. However, as long as we have at least the sum total memory available (i.e., d - already used >= total for client), we can always take all photos because at each step we just need enough memory for the next photo; the worst-case required free memory at any step is max(a,b) maybe? But if we start with enough free memory to cover the whole client's total, then at any point, the remaining free memory is at least the sum of sizes of photos not yet taken. But we need to ensure that before taking each photo, there is at least a (or b) free memory. Since a,b are positive, and the remaining free memory is decreasing, but if initial free memory >= total needed, then at any point before taking the next photo, free memory >= total needed - already taken, which is at least the size of the largest remaining photo (since all sizes positive). Actually, is it guaranteed that the free memory before taking a particular photo is at least the size of that photo? Suppose total needed = ax + by. We start with free = F >= total. After taking some photos, free = F - taken_sum. The next photo size is either a or b. The remaining total needed = total - taken_sum. Since taken_sum ≤ total, we have free = F - taken_sum ≥ total - taken_sum = remaining needed. The remaining needed includes the size of the next photo plus others, so remaining needed >= size of next photo. Therefore free >= size of next photo. So indeed if we have enough total free memory to accommodate the whole client, we can take them in any order. So serving a client is equivalent to checking that the remaining memory (d - used) is at least cost_i.
Thus the greedy by smallest cost is correct.
Therefore solution: read n, d, a, b; for i=1..n, read x,y, cost = ax + by; store (cost, i). Sort by cost. Iterate sum=0, count=0, selected indices. While sum + cost ≤ d, add, increment count, push index. Print count and selected indices.
Complexities: O(n log n) time, O(n) memory.
Edge cases: cost can exceed d individually; we skip them automatically. But sum of many small ones could exceed d.
Potential pitfalls: cost could be large up to ~2e9, sum could exceed 64-bit? d up to 1e9, sum of up to 1e5 items each up to 1e9? Actually each cost can be up to 2e9, but if cost > d, we won't include; but we might include many small ones whose sum could exceed 1e9? Since d <= 1e9, the sum of selected ones is at most 1e9, so int64 safe, but better to use 64-bit (long long).
Now, is there any scenario where picking smallest cost is not optimal because we need to consider that each client requires both x and y? No, cost captures total memory.
Thus solution is straightforward.
But wait, problem constraints: 1 ≤ a ≤ b ≤ 10^4, x_i, y_i up to 10^5, so cost up to 10^410^52 = 2e9, which fits in 32-bit signed? 2e9 ~ 2,000,000,000 which is just under 2^31-1 (2,147,483,647) so fits in signed 32-bit int. But sum may exceed 2^31, but since d ≤ 1e9, sum ≤ 1e9, fits in 32-bit signed. However, use 64-bit to be safe.
Now, we need to output indices in any order. So we can just output the ones we selected from sorted order.
Thus answer.
But hold on: The problem statement might have nuance: "Valera also does not delete photos from the camera so that the camera's memory gradually fills up." This hints that the order of serving clients may affect ability to serve later clients because memory gets filled cumulatively. But we already accounted for that by cumulative sum. The greedy by smallest cost yields maximum count irrespective of order, because any subset can be served in order of increasing cost (or any order) without affecting total sum. Since memory is cumulative, if we have a set of clients, we can serve them in any order as long as after each we have enough memory; but if we have a set of clients, the condition to serve all of them is that sum of costs ≤ d. However, is it always possible to order the clients such that at each step the memory used does not exceed d? Since we add costs cumulatively, if total sum ≤ d, then serving them in any order will always keep cumulative sum ≤ d, because cumulative sum increases monotonically and never exceeds total sum which is ≤ d. So no problem.
Thus solution is sound.
Double-check: Could there be a scenario where two clients have same cost but one has higher x and y but cost same? No, cost is linear.
Thus answer.
But the problem might be from Codeforces (CF 369B? Actually I recall problem "Photographer" or something). The solution is indeed sort by cost.
Thus we can implement.
Now, we must write final answer: Provide solution in C++? The user didn't specify language, but typical answer expects explanation and algorithm. They may want code? The instruction: "Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients." We need to output a program? Usually, the response should include reasoning and then final code (if applicable). The user asked: "Valera's lifelong ambition..." It's a problem statement. Possibly the user wants the solution explanation. Since it's a typical programming contest problem, the answer could include explanation and code.
Thus in the final answer, we will describe algorithm and provide C++ implementation.
Now, let's produce final response: Explanation: The memory needed for client i is ax_i + by_i. Since memory is cumulative, the problem is to select as many clients as possible with total memory ≤ d. To maximize count, we take clients with smallest memory requirements. Sort clients by this cost, take prefix until capacity exceeded. Output count and indices.
Complexities: O(n log n) time, O(n) space.
Now, code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int n, d, a, b;
cin >> n >> d >> a >> b;
vector<pair<ll, int>> clients(n); // cost, index
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
ll cost = (ll)a * x + (ll)b * y;
clients[i] = {cost, i+1};
}
sort(clients.begin(), clients.end());
vector ans;
ll sum = 0;
for (auto &p : clients) {
if (sum + p.first <= d) {
sum += p.first;
ans.push_back(p.second);
} else {
break;
}
}
cout << ans.size() << endl;
for (int idx : ans) {
cout << idx << " ";
}
cout << endl;
return 0;
}
That's it.
But careful: cost might exceed d individually, but we still might include if sum with previous small ones fits? Actually if p.first > d - sum, then we break; but if p.first > d but sum is 0? Then sum+p.first > d, break; so we won't include any client with cost > d. That's fine.
Now, we need to output numbers in any order. Since we output in sorted order, it's fine.
Thus final answer. We need to serve as many clients as possible given that each client i requires xi low‑quality photos (each uses a MB) and yi high‑quality photos (each uses b MB). The total memory needed for client i is cost_i = a·xi + b·yi. The camera has d MB of memory, initially empty, and photos are never deleted, so the memory used is cumulative. Serving a set of clients is possible iff the sum of their costs ≤ d. The goal is to maximise the number of clients served.
To maximise the count, we should take the clients with the smallest costs. Indeed, if we sort all clients by cost, the prefix of length k has the smallest possible total cost for any k clients. Therefore the largest k for which the sum of the first k sorted costs ≤ d is optimal.
Algorithm
- Read n, d, a, b.
- For each client i (1‑indexed), read xi, yi and compute cost_i = a·xi + b·yi (use 64‑bit integers to avoid overflow).
- Store pairs (cost_i, i).
- Sort the pairs by cost_i ascending.
- Iterate through the sorted list, accumulating the costs while the total does not exceed d. Collect the indices of taken clients.
- Output the number of taken clients and their indices (in any order).
Complexity
- Time: O(n log n) for sorting (n ≤ 10⁵).
- Space: O(n) for storing the clients.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, d, a, b;
cin >> n >> d >> a >> b;
vector<pair<ll, int>> clients(n);
for (int i = 0; i < n; ++i) {
int x, y;
cin >> x >> y;
ll cost = (ll)a * x + (ll)b * y;
clients[i] = {cost, i + 1};
}
sort(clients.begin(), clients.end());
vector<int> answer;
ll total = 0;
for (auto& [cost, idx] : clients) {
if (total + cost <= d) {
total += cost;
answer.push_back(idx);
} else {
break;
}
}
cout << answer.size() << '\n';
for (int idx : answer) {
cout << idx << ' ';
}
cout << '\n';
return 0;
}