In one little known, but very beautiful country called Waterland, lives a lovely shark Valerie. Like all the sharks, she has several rows of teeth, and feeds on crucians. One of Valerie's distinguishing features is that while eating one crucian she uses only one row of her teeth, the rest of the teeth are "relaxing".
For a long time our heroine had been searching the sea for crucians, but a great misfortune happened. Her teeth started to ache, and she had to see the local dentist, lobster Ashot. As a professional, Ashot quickly relieved Valerie from her toothache. Moreover, he managed to determine the cause of Valerie's developing caries (for what he was later nicknamed Cap).
It turned that Valerie eats too many crucians. To help Valerie avoid further reoccurrence of toothache, Ashot found for each Valerie's tooth its residual viability. Residual viability of a tooth is a value equal to the amount of crucians that Valerie can eat with this tooth. Every time Valerie eats a crucian, viability of all the teeth used for it will decrease by one. When the viability of at least one tooth becomes negative, the shark will have to see the dentist again.
Unhappy, Valerie came back home, where a portion of crucians was waiting for her. For sure, the shark couldn't say no to her favourite meal, but she had no desire to go back to the dentist. That's why she decided to eat the maximum amount of crucians from the portion but so that the viability of no tooth becomes negative.
As Valerie is not good at mathematics, she asked you to help her to find out the total amount of crucians that she can consume for dinner.
We should remind you that while eating one crucian Valerie uses exactly one row of teeth and the viability of each tooth from this row decreases by one.
Input
The first line contains three integers n, m, k (1 ≤ m ≤ n ≤ 1000, 0 ≤ k ≤ 106) — total amount of Valerie's teeth, amount of tooth rows and amount of crucians in Valerie's portion for dinner. Then follow n lines, each containing two integers: r (1 ≤ r ≤ m) — index of the row, where belongs the corresponding tooth, and c (0 ≤ c ≤ 106) — its residual viability.
It's guaranteed that each tooth row has positive amount of teeth.
Output
In the first line output the maximum amount of crucians that Valerie can consume for dinner.
Examples
Input
4 3 18
2 3
1 2
3 6
2 3
Output
11
Input
2 2 13
1 13
2 12
Output
13
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We have a problem: "Waterland" shark with teeth and rows. Let's restate clearly:
We have n teeth total, belonging to m rows. Each tooth i has row index r_i (1..m) and residual viability c_i (the number of crucians that can be eaten using that tooth before it becomes negative? Actually: "residual viability of a tooth is a value equal to the amount of crucians that Valerie can eat with this tooth." So the tooth can be used at most c_i times? But careful: Every time she eats a crucian, she uses exactly one row of teeth, and the viability of each tooth in that row decreases by 1. So if she uses a particular row, all teeth in that row get decremented by 1. So she cannot choose which tooth within the row to use; the whole row is used together. So the viability constraints are per tooth: For each tooth, the number of times its row is selected cannot exceed its viability c_i, otherwise that tooth's viability becomes negative (i.e., it's not allowed). The goal: maximize the total number of crucians eaten, but there is also a given total number of crucians available, k, which is the portion size? Actually: "a portion of crucians was waiting for her... she decided to eat the maximum amount of crucians from the portion but so that the viability of no tooth becomes negative." So there are k crucians available, but she might not be able to eat all due to teeth constraints. So the answer is the maximum number of crucians she can eat such that for each tooth, the number of times its row is used <= its viability c_i. And she can choose which row to use for each crucian arbitrarily? Yes, for each crucian, she picks a row to use. So essentially, we have m rows, each row can be used a certain number of times, but there is per-tooth constraints: For each row, the number of times that row is used cannot exceed the minimum viability among teeth in that row? Actually careful: Suppose a row has several teeth, each with viabilities c_i. When we use the row, we decrement all teeth in that row by 1. So after x uses of row j, each tooth i in row j has its viability reduced by x. So we require that for each tooth i, x_{r_i} ≤ c_i, where x_j is the number of times row j is used. So the constraint per tooth is that the total usage of its row must be ≤ its c_i. So the feasible usage counts x_j for rows j=1..m must satisfy: for each tooth i, x_{r_i} ≤ c_i. That is equivalent to: for each row j, x_j ≤ min_{i: r_i=j} c_i. Because all teeth in the row impose a bound, and the tightest is the minimum c_i in that row. But wait: is it that simple? Yes, because for each tooth in row j, we need x_j ≤ c_i. So for all i in row j, x_j ≤ c_i, so x_j ≤ min_{i in row j} c_i. Conversely, if x_j ≤ min c_i, then it's ≤ each c_i, so all constraints satisfied. So effectively, each row j has a capacity equal to the minimum viability among its teeth. However, there is also possibility that some teeth have larger c_i but that doesn't matter, the constraint is the smallest.
But there's a nuance: The description: "Every time Valerie eats a crucian, viability of all the teeth used for it will decrease by one. When the viability of at least one tooth becomes negative, the shark will have to see the dentist again." So we want to avoid any tooth viability negative after eating some crucians. So we can eat up to the point where some tooth would become negative if we continue. So if we decide to eat a certain number of crucians using rows, the final viability of tooth i is c_i - x_{r_i}. Must be >=0. So same bound: x_{r_i} ≤ c_i. So indeed each row j's total usage x_j must be ≤ c_i for all i in row j → x_j ≤ min_{i in row j} c_i.
But wait: Could we possibly exploit the fact that teeth within a row might have different viabilities, and we might use some teeth beyond their min if we stop using the row when that tooth becomes zero? No, because using the row reduces all teeth simultaneously. So we cannot avoid reducing the weaker teeth each time. So the row's capacity is the minimum viability among its teeth.
But is there any interaction across rows? Since each crucian uses exactly one row, and we can choose which row each time, the total number of crucians eaten is sum_j x_j. We want to maximize sum_j x_j subject to 0 ≤ x_j ≤ cap_j, where cap_j = min_{i in row j} c_i, and also sum_j x_j ≤ k because there are only k crucians in the portion. So the answer is min( k, sum_j cap_j ). Because we can just eat all crucians up to either the total capacity of rows (sum of minima) or the available k, whichever is smaller.
But example 1: n=4, m=3, k=18. Teeth:
(2,3)
(1,2)
(3,6)
(2,3)
Rows:
Row1: teeth with c=2 (only one tooth) -> cap1 = 2.
Row2: teeth with c=3 and 3 (two teeth) -> min=3, cap2=3.
Row3: teeth with c=6 (one tooth) -> cap3=6.
Sum caps = 2+3+6=11. k=18, so answer = min(18,11)=11. That matches output 11.
Example 2: n=2, m=2, k=13. Teeth:
(1,13)
(2,12)
cap1 = 13, cap2 = 12, sum = 25, k=13, answer min(13,25)=13. Output 13. So it's consistent.
Thus the problem seems trivial? But the constraints and description might hide a trick? Let's read problem statement carefully again to ensure we didn't miss any nuance: "For a long time our heroine had been searching the sea for crucians, but a great misfortune happened. Her teeth started to ache, and she had to see the local dentist, lobster Ashot. As a professional, Ashot quickly relieved Valerie from her toothache. Moreover, he managed to determine the cause of Valerie's developing caries (for what he was later nicknamed Cap). It turned that Valerie eats too many crucians. To help Valerie avoid further reoccurrence of toothache, Ashot found for each Valerie's tooth its residual viability. Residual viability of a tooth is a value equal to the amount of crucians that Valerie can eat with this tooth. Every time Valerie eats a crucian, viability of all the teeth used for it will decrease by one. When the viability of at least one tooth becomes negative, the shark will have to see the dentist again. Unhappy, Valerie came back home, where a portion of crucians was waiting for her. For sure, the shark couldn't say no to her favourite meal, but she had no desire to go back to the dentist. That's why she decided to eat the maximum amount of crucians from the portion but so that the viability of no tooth becomes negative. As Valerie is not good at mathematics, she asked you to help her to find out the total amount of crucians that she can consume for dinner. We should remind you that while eating one crucian Valerie uses exactly one row of teeth and the viability of each tooth from this row decreases by one."
Yes. So indeed the maximum is simply sum over rows of min viability in that row, capped by k. However, is there any additional constraint such as: she must use rows in some order? Or maybe each row can be used at most its min viability, but if there are more teeth with higher capacities, they might impose a different bound if we think about "remaining viability" after some uses? But no: the bound per row is the min. Because the smallest tooth determines the max uses.
But wait: Could there be a scenario where using rows in a different order could allow more than sum of mins? For example, consider two rows that share teeth? No, teeth belong to exactly one row. There's no overlapping. So rows are independent. So the total crucians she can safely eat without any tooth going negative is simply sum over rows of the minimum c_i in that row, because we can use each row up to that many times. And we can choose any order; we could also potentially stop using a row before its min capacity to allocate more to other rows? That would only reduce total. So max sum is sum of mins. So it's trivial.
But then why the problem? It seems too simple. Maybe the twist: The "residual viability" is the amount of crucians that can be eaten with this tooth. That is, the tooth can be used that many times before it decays? But maybe the interpretation is wrong: It might be that each tooth has a residual viability (initial viability), and each time a crucian is eaten using a row, the viability of each tooth in that row decreases by 1. So yes. So per row, we have a vector of viabilities. We want to maximize number of crucians subject to: For each tooth, number of times its row is used ≤ c_i. So indeed the row cap is min c_i.
However, the problem might be that she can decide, for each crucian, which row to use, but maybe she must use the rows in a certain order? No.
Given the sample inputs, it's exactly that. The problem may be intended as a simple introductory problem; the constraints are up to n=1000, k up to 1e6, which is small. But indeed, the solution is O(n). So maybe there is something else.
Let's search my memory: I recall a Codeforces problem named "Valera and ..."? Actually there's a Codeforces problem 489B? Not sure. But there is a problem about sharks and teeth and rows? I think this is from Codeforces Round? Possibly problem A? Might be from Codeforces 93A? I'm not sure. But regardless, it's likely a simple problem.
But why mention that each tooth row has positive amount of teeth? That ensures the min is defined. And the input format: n, m, k. Then n lines each with r and c. So we compute for each row the minimum c. Then answer = min(k, sum of minima).
But wait: Could there be a scenario where we can eat more than sum of minima by not using some rows fully? Obviously not, because sum of minima is the maximum feasible total uses across all rows, because you can use each row exactly up to its min and that's feasible, and you cannot exceed min for any row because then at least one tooth in that row would go negative. So sum of mins is the total capacity. So indeed it's the maximum.
Edge cases: c_i can be 0, so row cap can be 0. That's fine. k can be 0.
But is there any possibility that we can exceed the sum of mins by using rows partially such that some tooth becomes zero, but we might still use other rows? But that's already counted. Or maybe we could use a row more than its min if we stop using it before some tooth becomes negative, but the min already caps the max usage. So no.
Thus solution: Read n,m,k. Initialize an array minCap for rows 1..m with a large number (or -1, then take min). For each tooth, read r, c. If c < minCap[r], update minCap[r] = c. After all, sum = Σ minCap[r] (r=1..m). Answer = min(k, sum). Output answer.
But wait: The problem says "It's guaranteed that each tooth row has positive amount of teeth." So we don't have empty rows, so minCap[r] will be assigned.
So this seems too trivial. However, I suspect there might be a nuance: The "residual viability of a tooth is a value equal to the amount of crucians that Valerie can eat with this tooth." Possibly meaning that each tooth can be used for that many crucians, but when using a row, only one tooth in that row is used? No: "while eating one crucian she uses only one row of her teeth, the rest of the teeth are 'relaxing'." And later: "while eating one crucian Valerie uses exactly one row of teeth and the viability of each tooth from this row decreases by one." So the row's teeth are all used. So that is clear.
Maybe the trick is that some teeth may belong to multiple rows? But the problem says each tooth belongs to a row (given r). So it's one row per tooth.
Thus the solution is straightforward.
But given that this is a problem from some contest, maybe the intended solution is not that trivial? Let's search my memory: I recall a problem "Shark" from Codeforces Round #... Actually there is a problem "Shark" 165A? Not. Possibly it's from "Codeforces Round #257 (Div. 2)"? Not. Wait, maybe it's from "2013-2014 ACM-ICPC, NEERC, Moscow Subregional Contest"? That seems plausible because they have quirky stories.
Alternatively, perhaps the interpretation is different: "residual viability of a tooth is a value equal to the amount of crucians that Valerie can eat with this tooth." Could it be that each tooth individually can be used to eat up to c_i crucians, but each crucian requires using exactly one tooth? But that would contradict "uses only one row of her teeth". But they clarify later: "while eating one crucian Valerie uses exactly one row of teeth and the viability of each tooth from this row decreases by one." So it's per row.
Maybe the twist: There are n teeth, each belongs to a row. But rows are not necessarily distinct? Actually each tooth belongs to a row, but we have m rows, and each row has at least one tooth. So rows are sets.
Maybe the problem is tricking us because we might think we need to consider each tooth individually, but actually the row's capacity is the minimum. So many novices might try to sum all c_i or something, but the answer is min of sum of minima and k. Could be a simple problem to test reading comprehension.
Given the examples, it matches.
Thus final answer: compute sum of minima per row, output min(k, sum).
But wait: In example 1, sum of minima = 11, but could there be a way to eat 12? Suppose we try to use row3 more than 6? That would exceed tooth with c=6, so not allowed. Row2 more than 3? Exceed its min 3. Row1 more than 2? Exceed 2. So indeed max total is 11. So correct.
Thus answer.
We must output a single integer.
Implementation details: n,m,k up to 1000, c up to 1e6, sum up to m*1e6 up to 1e9? Actually m ≤ n ≤ 1000, so sum up to 1e9, but k up to 1e6, so sum could be larger than int32, but within 64-bit. So use long long.
Edge: if we initialize minCap array with a large number like INF = 1e9+5. Or we can set minCap[r] = min(minCap[r], c) with initial large value.
Thus code is trivial.
But maybe the problem is more complicated because the rows are not independent? Let's double-check: The condition is that for each tooth, the number of times its row is used ≤ c_i. This defines a feasible region: for each row j, x_j ≤ c_i for each tooth i in row j. So x_j ≤ min_i c_i. So indeed independent. So maximum total = sum of mins. No other constraints.
But could we perhaps use a row more than its min if we stop using it before the weakest tooth gets to zero, but then use some other teeth? No, the condition must hold for all teeth at the end. So we just need final x_j ≤ c_i for all i. So it's exactly the above.
Thus answer.
I'm confident.
But maybe there's a twist: "the viability of all the teeth used for it will decrease by one." Could it be that not all teeth in the row are used? The phrase "each tooth from this row" suggests all teeth in that row are used. So yes.
Therefore final answer: compute mins, sum, output min(k, sum).
But wait: There might be a nuance: She might be able to eat a crucian using a row and then later switch rows, but the viability decreases each time. The total number of times each row can be used is bounded by the smallest c_i in that row. So that's it.
So the solution is simple.
Given the instruction: "You are an AI" and the problem is from a codeforces-like environment. The solution I'll provide: read input, compute per-row min, sum, output min(k, sum).
But just to be thorough, let's consider if there is any possibility that we could exceed the min per row by using rows in combination? Suppose a row has teeth with viabilities: tooth A = 2, tooth B = 100. The min is 2, so max uses of that row is 2. But could we use that row for 100 crucians if we also occasionally use some other rows to "rest" the tooth A? No, because every time we use that row, A decreases. There's no way to increase A again. So you can't exceed 2 uses without A going negative.
Thus correct.
Thus answer is straightforward.
Now I should produce final answer: Provide solution explanation and code.
But the user likely expects just the answer? The prompt says: "You are an AI... Provide a well-reasoned, step-by-step solution, and then give the final answer." This is typical for these problems. So I'll output a solution description and then code.
But the final answer they want is the maximum amount of crucians? Actually the output is a number. But as part of the solution they might expect the code or just the answer? The typical format: In these problem-solving contexts, the user says "In one little known..." and then expects a solution. Usually the ChatGPT outputs the reasoning and then the final answer as a number? But often they want the code? The problem statement looks like from a contest, and the user is asking for help to find out the total amount. Possibly they just want the answer? But the instructions: "In the first line output the maximum amount of crucians that Valerie can consume for dinner." So if this were a direct question, they'd want the answer. But the system says "Provide a well-reasoned, step-by-step solution, and then give the final answer." So I'll include both: reasoning, algorithm, and then output the number? But the example output is 11, 13. So the answer for given input would be computed. However the user didn't provide a specific input; they provided the problem statement and sample inputs. They want me to produce a solution that would work for any input? Usually the task is to write a solution to the problem. So I'll produce an algorithmic solution and code.
Thus final answer: code in C++ (since typical). Or Python. I can do both.
I'll present step-by-step reasoning, then code in C++.
Potential pitfalls: Use 64-bit integer. Input reading. Computing min per row.
Complexity O(n).
Now produce final response.
```cpp
#include
#include
#include
using namespace std;
int main() {
int n, m, k;
cin >> n >> m >> k;
code
// Initialize minCap for each row to a large value.
vector<long long> minCap(m + 1, 1e9); // rows are 1-indexed
for (int i = 0; i < n; ++i) {
int r, c;
cin >> r >> c;
if (c < minCap[r]) {
minCap[r] = c;
}
}
long long totalCap = 0;
for (int r = 1; r <= m; ++r) {
totalCap += minCap[r];
}
long long ans = min(totalCap, (long long)k);
cout << ans << endl;
return 0;