We need to find the smallest positive integer (starting from 1) up to 10^17 that cannot be formed as a sum of a subset of the given coins (with repetitions? Typically, coins are given as denominations, each coin can be used at most once? The problem statement: "Ned Stark is the Lord of Winterfell. He gives Daenerys some coins. Each coin has its own denomination (in Rs). The coins need not be of distinct denomination. He asks Daenerys to predict the cheapest product that she will not be able to buy from the market using the coins given to her." Usually, in such problems, you are given a set of coins (each coin can be used at most once). But sometimes it could be that you have unlimited usage? However, they say "the coins given to her" and "using the coins given to her" implies that each coin is a physical item that can be used once. Also sample: first test case: coins are 1,2,4,...,512 => N=10. That set can represent any sum from 0 to 1023 (binary). The smallest unattainable is 1024. That matches using each coin at most once. If unlimited usage? With unlimited usage, you could always make any amount with 1 coin of denomination 1, but there is only one 1 coin, so you cannot make 2 if you have only one 1? Wait, if you have unlimited usage of denominations, you could use the same coin multiple times? No, physical coins cannot be reused after spending; but sometimes problems treat it as a multiset of coins. The phrase "each coin has its own denomination" and "the coins need not be of distinct denomination" means you might have multiple coins of same value. So it's a multiset. And you can use each coin at most once. So it's the classic "smallest positive integer that cannot be represented as a sum of a subset of given numbers" given the numbers are sorted and positive. This is known as the "smallest impossible sum" problem. The typical solution: sort the coin denominations, then maintain a variable "reachable" which is the maximum sum we can form using a subset of the processed coins for all sums from 0 to reachable. Initially reachable = 0. For each coin value a in sorted order, if a > reachable + 1, then reachable+1 is the smallest unattainable sum. Otherwise, we can extend reachable to reachable + a. Continue until we either find a gap or exhaust all coins. If we can cover up to some high value, maybe up to 10^17, then output -1.
The constraints: N up to 1e5 per test case, T up to 100 => total N up to 1e7? That could be large but maybe total sum of N across test cases is up to 1e5? Usually it's per test case but we should be careful. However, the worst-case total N across tests might be up to 1e7 (100 * 1e5 = 1e7) which is acceptable with efficient I/O and O(N log N) sorting per test case. Also note that the maximum sum of coin values could be up to N * 1e9 = 1e14, but we need to consider up to 10^17. So we need to use 64-bit integers (long long in C++). The reachable can go up to 10^17, but we may stop early if reachable >= 10^17? Actually the requirement: if she can buy all products from the market (i.e., all products up to price Rs 10^17), then output -1. So we only need to check up to 10^17. So if after processing all coins, reachable >= 10^17, we output -1. But careful: reachable being the maximum contiguous sum starting from 1? Actually we need to find the smallest positive integer not representable. The algorithm: maintain "max_reachable" meaning we can make all sums from 0 to max_reachable. Initially max_reachable = 0 (we can make 0 with empty set). For each coin a (sorted), if a <= max_reachable + 1, then we can extend max_reachable to max_reachable + a (because using a we can make all sums from a to a+max_reachable, and since a <= max_reachable+1, there is overlap, so we can cover all up to max_reachable + a). If a > max_reachable + 1, then we cannot make max_reachable+1, so answer is max_reachable+1. After processing all coins, if we never found a gap and max_reachable >= 10^17? Actually if we can make all sums up to max_reachable, but there might be a gap beyond that. The market has products up to 10^17. So if max_reachable >= 10^17, then we can buy all products up to 10^17 (since any product price p in [1,10^17] is <= max_reachable, so it's representable). So output -1. If max_reachable < 10^17, then the smallest product she cannot buy is max_reachable + 1. But careful: is it guaranteed that after processing all coins, we can make all sums up to max_reachable? The algorithm's invariant holds: after processing i sorted coins, we can make all sums from 0 to max_reachable, provided the condition a_i <= max_reachable+1 each time. If at some point we skip? Actually we sort and process each coin; if a > max_reachable+1, we break and answer max_reachable+1. So if we process all coins without breaking, then we have extended max_reachable to total sum? Possibly total sum could be less than 10^17, but we can make all sums up to total sum? Let's verify: The algorithm ensures that after processing all coins (with the condition a <= max_reachable+1 for each), we can make all sums up to the total sum. This is standard. So if total sum >= 10^17, but note that max_reachable might become total sum (since we add all a's). But if we have a scenario where some coin is larger than max_reachable+1, we would break early, and answer would be something < that coin. So if we never break, then all coins satisfy a <= max_reachable+1 at the time, so after adding all, max_reachable = sum of all coins. And we can make all sums up to total sum. So if total sum >= 10^17, then we output -1; otherwise answer is total sum + 1? Wait, careful: If we process all coins without breaking, then max_reachable becomes the total sum of all coins. And we can make all sums from 0 to total sum. So if total sum < 10^17, then the smallest unattainable is total sum + 1. If total sum >= 10^17, then we can buy all products up to 10^17, so output -1. But there is nuance: Could it be that total sum >= 10^17 but there is a gap before 10^17? Since the algorithm would not break only if each a <= max_reachable+1. That ensures contiguous coverage. So if total sum >= 10^17, then certainly max_reachable >= 10^17? Actually max_reachable is the total sum. So total sum >= 10^17 => max_reachable >= 10^17. So we can cover all up to 10^17. So -1.
Edge cases: The array can have values up to 1e9, but we need 64-bit for sum. Also note: The market has products worth Rs 1,2,3,... up to 10^17. So answer must be between 1 and 10^17+1? Actually if we cannot buy any product, say no coin 1, then answer is 1. That's fine.
Now we must check sample: first test case: coins are powers of 2. Sorted: 1,2,4,...,512. Process: max_reachable=0; a=1 <=1 -> max_reachable=1; a=2 <=2 -> max_reachable=3; a=4 <=4 -> max_reachable=7; ... After 512, max_reachable=1023. Next no coin. max_reachable=1023 < 10^17. Answer = 1024. OK.
Second test: 1,3,4,5,7 sorted. Process: max_reachable=0; a=1 <=1 -> max_reachable=1; a=3 >2? max_reachable+1=2, a=3 >2 => break, answer=2. OK.
So algorithm is correct.
Now constraints: T up to 100, N up to 1e5 per test. So we need to read input quickly. Use scanf/printf or fast I/O in C++. Also use long long.
Potential pitfalls: The coins may not be sorted; we need to sort. Sorting 1e5 numbers per test is okay.
Also note that we need to stop early if max_reachable already >= 10^17? Actually if during processing we exceed 10^17, we could break and output -1, but we must be careful: we cannot break early just because max_reachable >= 10^17, because we might still have coins that cause a gap? No, if we already have max_reachable >= 10^17, then we can already make all sums up to 10^17 (since invariant says we can make all sums from 0 to max_reachable). But that invariant only holds if we processed all previous coins satisfying condition. So if we are at some point, max_reachable >= 10^17 and we haven't broken before, then indeed we can make all sums up to 10^17. Even if there are remaining coins, we don't need to process them because answer is -1. However, careful: Could a later coin disrupt the ability to make sums? No, adding more coins can only increase the set of sums, but it cannot cause us to lose ability to make sums we already could. So if we already have max_reachable >= 10^17, we can safely output -1 without processing the rest. But we need to ensure that indeed we can make all sums up to 10^17. The invariant holds: after processing some coins, we can make all sums from 0 to max_reachable. So if max_reachable >= 10^17, then yes. So we can break early and output -1. That could save time if total sum is huge. But it's not necessary.
Edge: if we have max_reachable >= 10^17 but we haven't processed all coins yet, we can just break and output -1. But we need to consider the case where max_reachable is huge but we haven't processed a coin that is > max_reachable+1? Actually if max_reachable is already >= 10^17, even if later we encounter a coin > max_reachable+1, that doesn't affect the ability to represent sums up to 10^17 because we already can. So answer is -1. So we can break as soon as max_reachable >= 10^17.
But careful: Could there be a situation where max_reachable is large but not all sums up to max_reachable are representable because some earlier condition was violated? But the algorithm only continues if at each step a <= max_reachable+1. If we have a step where a > max_reachable+1, we would break early before reaching that max_reachable. So if we have max_reachable >= 10^17, that must have been achieved without any gap, i.e., all steps satisfied condition. So it's fine.
Thus we can implement:
for each test case:
read N
read array A of length N
sort A
long long max_reachable = 0
for i from 0 to N-1:
if A[i] > max_reachable + 1:
answer = max_reachable + 1
break
else:
max_reachable += A[i]
if max_reachable >= 1e17:
answer = -1 (or set a flag)
break (but need to skip further processing)
after loop:
if not broken and max_reachable < 1e17: answer = max_reachable + 1
else if not broken and max_reachable >= 1e17: answer = -1
But careful: If we break due to max_reachable >= 1e17, we set answer = -1. If we break due to gap, we already have answer.
Now we need to handle large numbers: 1e17 fits within 64-bit (2^63-1 ~9.22e18). So long long is fine.
Now some nuance: The market has products from 1 to 10^17. If max_reachable is >= 10^17, we can buy all. So answer -1. If max_reachable is less, answer is max_reachable+1.
Now consider extreme case: N=0? But N≥1 per constraints? Actually N≥1 (1 ≤ N). So fine.
Now complexity: O(T * N log N) due to sorting. With T up to 100 and N up to 1e5, total sorting cost up to 100 * 1e5 log 1e5 = about 1e7 * log(1e5) ~ 1e7*17 = 1.7e8 operations, borderline but probably okay in C++ with optimization? Actually 100 * 1e5 = 10^7 items. Sorting each test case individually: if T=100 and each N=1e5, sorting 100 arrays of size 1e5 each. That is total 100 * (1e5 log2 1e5) ~ 100 * 1e5 * ~17 = 170 million comparisons. That's high but may still pass if well-optimized (C++ sort is fast). However, maybe total N across test cases is limited? The problem statement says 1 ≤ N ≤ 10^5 for each test case, but doesn't specify total sum. In many problems, they ensure total N across test cases is ≤ something, but not here. Could be that worst-case T=100 and each N=1e5, leading to 10 million numbers. Sorting 10 million numbers overall is okay. But sorting 100 arrays each of size 1e5 is not same as sorting 10 million numbers as one array; it's 100 sorts of 1e5. That's maybe 1-2 seconds. We'll assume it's fine.
Alternatively, we could use partial sort? But sorting is typical.
Potential overflow: sum of A[i] might exceed 1e17. We must cap max_reachable at maybe 1e18? But we only compare with 1e17. Adding a bunch could cause overflow if we don't cap. Since N up to 1e5, each A[i] up to 1e9, total sum up to 1e14, actually 1e5 * 1e9 = 1e14, which is less than 1e17. So total sum across all coins is at most 1e14, which is less than 1e17. Wait: N max = 1e5, A_i max = 1e9, so maximum sum = 1e14. That is less than 1e17. So we cannot reach 1e17 with the sum of given coins. That means we will never have max_reachable >= 1e17 from simply adding all coins. But wait: Could we have max_reachable exceed 1e17 through addition? Since total sum max is 1e14, it's impossible. So the condition max_reachable >= 1e17 will never be true. However, the market goes up to 1e17, but we only have coins sum up to 1e14. So we can never cover all products up to 1e17. That means we will never output -1? But the problem says "If she can buy all products from the market (i.e all products upto price Rs 10^17 ), then output -1." But with given constraints, total sum is at most 1e14, so it's impossible to cover up to 1e17 unless we have some other property? Actually to cover all sums up to 1e17, we need that the set of coins can represent every integer up to 1e17, not necessarily that the sum of all coins is at least 1e17. It's possible to have sum of coins less than 1e17 but still cover all numbers up to 1e17? Let's think: If we have coins, each can be used at most once, the maximum sum we can achieve is the sum of all coins. To represent a number M, we need a subset sum = M. So if M > total sum, it's impossible. So to represent all numbers up to 10^17, total sum must be at least 10^17. So with constraints, total sum <= 1e14, so impossible. Therefore, output -1 can never occur under given constraints. Wait, is that correct? Let's check constraints: 1 ≤ N ≤ 10^5, 1 ≤ A_i ≤ 10^9. So max total sum = 10^5 * 10^9 = 10^14. That is indeed < 10^17. So indeed it's impossible to have total sum >= 10^17. But the problem might have intended that N can be larger? Perhaps they meant A_i up to 10^17? Or maybe they meant product prices up to 10^17, but you are not required to have sum of coins >= 10^17; the condition for -1 is that she can buy all products up to 10^17, meaning for each price p in [1,10^17], there exists a subset sum equal to p. Since maximum subset sum is total sum of all coins, to represent 10^17, total sum must be at least 10^17. So yes, -1 can never happen under given constraints. However, sample and problem description mention output -1, but with constraints, -1 is unreachable. Could it be that they allow using coins multiple times? If unlimited usage, then with a coin of denomination 1, you can make any amount regardless of number of coins, but the problem says "the coins given to her", implying limited number. Unlimited usage is not implied. Yet, the constraints of N and A_i would then allow -1 if there is a coin of 1? But with unlimited usage, you could make any amount with just one 1 coin (by using it repeatedly). But the phrase "using the coins given to her" suggests you only have those physical coins, you can't magically duplicate them. So I think it's subset sum.
But then, why would they mention output -1 and set product prices up to 10^17 if -1 is impossible? Possibly they originally intended that N can be up to 10^5 but A_i can be up to 10^12 or something, or maybe they allow using each coin any number of times? Let's check typical problem: "smallest positive integer that cannot be represented as sum of a subset of given array" is known as "Find the smallest positive integer that cannot be represented as sum of any subset of given array". Usually the answer can be up to sum+1. The -1 case is when you can represent all numbers from 1 to sum. But there is also a version where you have unlimited coins of each denomination? That would be the classic coin problem (canonical coin system) but then the answer is something else. The sample second case: coins 1 3 4 5 7 -> answer 2. With unlimited usage of these denominations, could we make 2? If unlimited, we could use the 1 coin twice? But we only have one coin of 1? Actually we have exactly one 1 coin? The list: 1,3,4,5,7. Unlimited usage would mean we can use each denomination as many times as we want. Then we could use 1 twice to make 2. But we only have a single 1 coin? Unlimited usage means we have infinite supply of each denomination? The problem says "He gives Daenerys some coins. Each coin has its own denomination. The coins need not be of distinct denomination." That implies a multiset of actual coins. So not infinite.
Thus, with subset sum, the second case answer 2 makes sense: with one 1, can't make 2. So that's consistent.
But then the -1 case: With 10 coins: powers of 2 up to 512, total sum = 1023. That covers all up to 1023, but not beyond. So answer 1024. No -1.
But the problem statement says "If she can buy all products from the market (i.e all products upto price Rs 10^17 ), then output -1." That implies there might be test cases where the total sum is >= 10^17, meaning N may be huge or A_i large enough that sum >= 10^17. However, given constraints, A_i <= 10^9, N <= 10^5, sum <= 10^14 < 10^17. So maybe constraints are not accurate in the problem statement? Or maybe they intended that A_i can be up to 10^17? Let's check: In the sample, A_i are small. But perhaps they intended N up to 10^5 but A_i up to 10^12? Or maybe the product price limit is 10^12? Actually 10^17 is huge. But perhaps they allow unlimited usage? If unlimited usage, then with a 1 coin you can make all numbers, so answer would be -1 if there is a coin of 1. But sample second case: there is a coin of 1, yet answer is 2, so unlimited usage is not the case.
Thus, either constraints are mis-typed, or the -1 condition is just a formality but never occurs. However, we must implement as per problem statement: we should still check if reachable >= 1e17. But given constraints, it won't happen. Yet maybe they have test cases that exceed the given constraints? Usually competitive programming problems stick to constraints. But it's possible that the constraints for A_i are 1 ≤ A_i ≤ 10^9 but N could be larger? But they say N <= 10^5. So sum <= 10^14.
Maybe they consider that you can use each coin any number of times? That would break the sum limitation because with a coin of denomination 1, even if you have only one coin of 1, you cannot use it more than once because it's a physical coin. So that's not.
Maybe the product prices start from 1 but go up to 10^17, but you are not required to cover all numbers up to 10^17 by subset sum; maybe you can use coins multiple times? But then the answer for sample 2 would be something else: With unlimited usage of denominations {1,3,4,5,7}, the smallest unattainable would be? Let's compute: classic coin problem (canonical coin system) smallest amount that cannot be made with unlimited coins. For denominations 1,3,4,5,7, can we make 2? No, because smallest is 1, but need another 1, but you don't have 1 again unless unlimited usage allows using 1 twice. Unlimited usage would allow using 1 twice, so 2 is makeable. So answer would not be 2. So unlimited usage is not the case.
Maybe it's "each coin can be used at most once, but there are multiple coins of each denomination"? That's what we have.
Thus, the -1 condition seems impossible. But we'll still implement it generically, using long long and checking if reachable >= 1e17. Since reachable will never exceed sum total which is at most 1e14, we never output -1. But maybe they have test cases where N is larger? In some problems, total N across test cases might be up to 10^5, not per test. But they said per test. However, we should still code for the general case.
Nevertheless, it's safer to handle the -1 condition correctly.
Now about I/O: Use scanf/printf. We'll read T. For each test case, read N, then read N integers. Sorting can be done via std::sort.
Now careful: Since we use long long for reachable, and we add A[i] which is up to 1e9, we might need to cast to long long.
Implementation in C++:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAX_PRICE = 100000000000000000LL; // 1e17
int main() {
int T;
scanf("%d", &T);
while (T--) {
int N;
scanf("%d", &N);
vector a(N);
for (int i=0; i<N; ++i) scanf("%d", &a[i]);
sort(a.begin(), a.end());
ll reachable = 0;
ll answer = -1;
for (int i=0; i<N; ++i) {
if (a[i] > reachable + 1) {
answer = reachable + 1;
break;
} else {
reachable += a[i];
// if reachable >= MAX_PRICE, we can already make all up to 1e17
if (reachable >= MAX_PRICE) {
answer = -1;
break;
}
}
}
if (answer == -1) {
// loop completed without break? Need to check.
if (reachable < MAX_PRICE) {
// didn't break and not set answer
answer = reachable + 1;
} else {
// reachable >= MAX_PRICE and we broke with answer=-1 already
// or loop finished with reachable >= MAX_PRICE? But if we broke due to gap, answer would be set.
// So if answer==-1 and loop didn't set answer from gap? We need to differentiate.
// Actually we set answer=-1 only when we break due to reachable>=MAX_PRICE.
// If loop finishes normally, answer remains -1 (initial). But then we need to output based on reachable.
// So we should have a flag.
}
}
}
}
Better to use a flag.
Simplify:
bool found = false;
ll reachable = 0;
for (int i=0; i<N; ++i) {
if (a[i] > reachable + 1) {
printf("%lld\n", reachable + 1);
found = true;
break;
}
reachable += a[i];
if (reachable >= MAX_PRICE) {
printf("-1\n");
found = true;
break;
}
}
if (!found) {
// processed all coins without breaking
if (reachable >= MAX_PRICE) printf("-1\n");
else printf("%lld\n", reachable + 1);
}
But note: if reachable >= MAX_PRICE after adding last coin, we would have broken earlier if we checked after each addition. But if it's exactly after last addition, we won't break because the check is after addition; but we need to output -1 if reachable >= MAX_PRICE after loop. So it's covered.
But careful: If after adding a coin we get reachable >= MAX_PRICE, we can output -1 immediately, but we need to ensure that we haven't broken due to gap. Since we only add if a[i] <= reachable+1, there is no gap. So it's safe.
Thus implementation:
for each test:
read N
read array
sort array
long long reach = 0;
int i;
bool all_coins_processed = true;
for (i = 0; i < N; i++) {
if (a[i] > reach + 1) {
printf("%lld\n", reach + 1);
all_coins_processed = false;
break;
}
reach += a[i];
if (reach >= MAX_PRICE) {
printf("-1\n");
all_coins_processed = false;
break;
}
}
if (all_coins_processed) {
// loop finished without break
if (reach >= MAX_PRICE) printf("-1\n");
else printf("%lld\n", reach + 1);
}
But note: if we break due to reach >= MAX_PRICE, we printed already. So all_coins_processed is false, no additional output. Good.
Now consider potential overflow: reach may become > 2^63-1? Maximum sum is 1e14, far less than 9e18. So safe.
Now check sample: Should output 1024 and 2.
Now edge cases:
-
If smallest coin > 1: reach initially 0, a[0] > 1 => answer 1. Good.
-
If all coins are 1: sorted [1,1,1,...] N times. Process: each a[i]=1 <= reach+1. Initially reach=0, a[0]=1 <=1, reach=1. Next a[1]=1 <=2, reach=2. ... After processing all N, reach = N. Then answer = N+1. That is correct: with N ones, can make any sum up to N, not N+1.
Now, do we need to consider product price up to 10^17? For N ones, max reachable = N. If N < 10^17, answer = N+1. That's fine.
Now, there's nuance: The algorithm assumes that we can use each coin at most once and we can choose any subset. That's correct.
Now, one might argue that we need to output -1 if the smallest unattainable is > 10^17? Actually the problem asks for the cheapest product she cannot buy. Products are up to 10^17. If she can buy all products up to 10^17, answer -1. So if reachable >= 10^17, answer -1. If reachable < 10^17, answer reachable+1 (which is <= 10^17? Possibly reachable+1 could be > 10^17? If reachable = 10^17 - 1, then answer = 10^17, but product exists up to 10^17, so answer is 10^17. If reachable = 10^17 exactly, answer -1. If reachable > 10^17, also -1. So our condition reachable >= 10^17 yields -1. Good.
Now, we need to ensure that reachable+1 could be > 10^17? For reachable = 10^17, we output -1, not 10^17+1. For reachable = 10^17 - 1, we output 10^17, which is within product range. So that's correct.
Now, consider if reachable is huge but <10^17, but reachable+1 may exceed 10^17? Actually if reachable < 10^17, reachable+1 <= 10^17. So fine.
Now, we need to think about large test cases. Sorting 1e5 ints is fine. But we must use fast I/O.
Potential improvement: Instead of sorting, we could use a multiset? Sorting is simplest.
Now, verify constraints: A_i up to 1e9, N up to 1e5. So total sum <= 1e14, but we are comparing to 1e17. So the condition reachable >= 1e17 will never trigger. But we keep it for completeness.
Now, what about T up to 100 and N up to 1e5 each, total 10 million integers to read. Use scanf which is okay. Sorting 100 arrays of size 1e5 each: 100*O(N log N) ~ 100 * 1e5 * 17 ~ 170 million comparisons, each comparison cheap. Should run within time if optimized.
Now, some might propose using counting sort or radix? Not needed.
Now, there is a nuance: The algorithm expects sorted order, but if there are duplicate values, it's fine.
Now, implement.
But before finalizing, consider possibility of integer overflow when adding reachable + a[i]. reachable is long long, a[i] is int, so fine.
Now, alternative: Could there be a case where a[i] is very large and break condition holds but we also have reachable already large? Works.
Now, I'll produce final code.
Edge: After sorting, if the smallest coin > 1, we output 1. But ensure that reachable+1 = 1, so fine.
Now, regarding the -1 output: If reachable >= 10^17, we output -1. But as argued, that never happens, but we include.
One more thing: The problem's product prices start from 1, and we consider 0 as not a product. So our method for smallest positive integer is correct.
Now, double-check the second sample: coins: 1 3 4 5 7 sorted: 1 3 4 5 7. Process: reach=0; a=1 <=1 -> reach=1; a=3 > reach+1=2 -> break, output 2. Correct.
Now, what about test case where after processing all coins reachable is huge but not enough to cover all products? For example, N=1, coin=10^9. reachable=0, a=10^9 > 1 -> output 1. That's correct.
Now, test case where coins are [1,2,3] => reachable after 1:1; 2<=2 -> reach=3; 3<=4 -> reach=6; answer=7 if sum<10^17. That's correct because with coins 1,2,3 we can make sums 1..6? Actually can we make 4? 1+3=4; 5=2+3; 6=1+2+3; yes. So 7 is smallest not possible.
Now, it's good.
Potential pitfalls: In C++, using vector for each test case may cause high allocation overhead if T=100 and N=1e5. But it's okay: 100 allocations of 1e5 ints ~ 40 MB? Actually 1e5 ints = 400KB, times 100 = 40 MB, but they are allocated sequentially, not all at once (since each test case is processed and then vector is destroyed). So memory usage peaks at about 400KB + overhead. That's fine.
Alternatively, we could use a static array of size max N (1e5) and reuse it. But vector is fine.
Now, let's implement.
Also need to include the constant 100000000000000000LL or 1e17. 1e17 is a double; better to use 100000000000000000LL or 1e17 as long long: 100000000000000000LL? Actually 10^17 = 100000000000000000 (that's 1 followed by 17 zeros). That's exactly 10^17. But 10^17 fits within 2^63-1? 2^63-1 = 9.22e18, so yes. But careful: 10^17 = 100,000,000,000,000,000. That's 17 digits. So we can define:
const long long LIMIT = 100000000000000000LL;
Alternatively, use 1e17, but 1e17 is a double literal, but can be cast to long long. But to be safe, use LL constant.
Now, code.
We'll use scanf and printf.
Now, potential bug: When we break due to gap and print, we must not print again. So we use a flag.
Now, I'll write:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAX_PRICE = 100000000000000000LL; // 10^17
int main() {
int T;
scanf("%d", &T);
while (T--) {
int N;
scanf("%d", &N);
vector a(N);
for (int i=0; i<N; ++i) scanf("%d", &a[i]);
sort(a.begin(), a.end());
ll reach = 0;
bool found_ans = false;
for (int i=0; i<N; ++i) {
if (a[i] > reach + 1) {
printf("%lld\n", reach + 1);
found_ans = true;
break;
}
reach += a[i];
if (reach >= MAX_PRICE) {
printf("-1\n");
found_ans = true;
break;
}
}
if (!found_ans) {
// processed all coins
if (reach >= MAX_PRICE) printf("-1\n");
else printf("%lld\n", reach + 1);
}
}
return 0;
}
Now, test on sample.
Input:
2
10
1 2 4 8 16 32 64 128 256 512
5
1 3 4 5 7
Processing first test: After sorting (already sorted). Loop: reach increments to 1023. At end, reach = 1023 < MAX_PRICE, loop ends without break. found_ans false, output 1024. Good.
Second test: After first coin, reach=1. Second coin=3 > reach+1=2, prints 2, break. Good.
Now, test edge: N=1, coin=1: reach after first:1, then check reach >= MAX_PRICE? 1<MAX_PRICE. Loop ends. found_ans false, output reach+1=2. That's correct: with one coin of 1, cannot buy product of price 2.
Now, test where coin=1e9 and others. Works.
Now, I think solution is correct.
But before finalizing, let's reconsider the -1 condition: Since total sum can't reach 1e17, -1 won't appear, but we kept it.
Now, is there any scenario where reachable could be >= 1e17 even if total sum < 1e17? No, reachable is sum of a subset (specifically all coins in sorted order) after processing, but it's exactly the sum of processed coins. Since total sum is sum of all coins, if total sum < 1e17, reachable cannot exceed total sum. So no.
Thus, -1 is impossible given constraints. But we'll still include.
Now, time complexity: Sorting each test case O(N log N). Input reading O(N). Memory O(N).
Now, note that using vector and sorting for each test case might be okay. Could we improve by not sorting? The algorithm requires sorted order. So sorting is necessary.
Now, check if any tricky case: The coin denominations could be huge and many, but sorted works.
Now, also note that we might encounter integer overflow in reachable + a[i] if reachable is near 9e18, but we stop at 1e17, so no overflow.
Now, let's think about the possibility of using each coin more than once? Re-evaluate if the problem might be interpreted differently: Could it be that the market sells products worth 1,2,3,... and Daenerys has some coins, and she can use these coins to pay exactly the price? Typically, when paying, you can use any combination of coins (like real money) and you can use each coin only once. So our interpretation is standard.
But check the note: "Partial marks are awarded if any test case passes." So perhaps some test cases are designed to be easy, but some may be large.
Now, I'll produce final answer.
However, I'm not entirely convinced about the -1 condition. Could it be that they consider the coins can be used unlimited times? If unlimited, then the answer is different: The classic problem "smallest amount that cannot be made with given denominations (with unlimited coins)" is solved by a similar greedy algorithm if the denominations are sorted and we maintain the smallest unreachable sum, but it works only when the denominations are such that each coin is <= current smallest unreachable. Actually that algorithm is used for the "minimum unattainable sum" problem when you have an unlimited supply of each denomination? Wait, there is a known problem: "Given a set of coin denominations (unlimited supply), find the smallest amount that cannot be formed." This is often solved by sorting and maintaining current reachable as the smallest unreachable? Actually it's similar but with unlimited coins, you can use each coin many times. The algorithm for unlimited coins (also known as the "canonical coin system" for checking if all amounts from 1 upwards are representable) is: sort denominations; let reachable = 0; for each coin in sorted order: if coin > reachable+1 then break; else reachable += coin * (infinite?) Actually if you have unlimited coins, you can use each coin as many times as needed. For the smallest unattainable sum, the algorithm is: sort coins; let max_reachable = 0; for each coin c: if c > max_reachable+1 then answer = max_reachable+1; else max_reachable += c; Wait, that's exactly the same! But is that valid for unlimited coins? Let's test: Coins: [2]. Unlimited supply. Sorted: [2]. Initially max_reachable = 0. First coin 2 > 1 => answer 1. That's correct: you cannot make 1 with 2's. But with unlimited supply, you can make 2,4,6,... but not 1. So answer 1. For coins [1,3] unlimited. Sorted: [1,3]. max_reachable=0; c=1 <=1 => max_reachable=1; c=3 <=2? 3 > 2, so answer = 2. Indeed with unlimited 1 and 3, you can make 1,3,4(1+3),5(1+1+3?), but 2? 2 = 1+1 but you have unlimited 1s, so wait! With unlimited 1, you can make 2 by using two 1's. But we only have one 1 coin in the set? Actually unlimited supply means you have infinite 1's, so you can make 2. So the answer should be not 2. But the algorithm would give 2. That is wrong. So the algorithm only works when each coin can be used at most once (or when the coin system is such that each coin is used at most once? Actually the algorithm works for the "subset sum" problem where each coin is used at most once, and also for the "coin change" problem where you have unlimited coins if the denominations are canonical? Let's examine: For unlimited coins, if we have denominations sorted, we maintain the smallest amount that cannot be formed with the coins seen so far, assuming we have unlimited supply of them? The typical method for the "minimum amount that cannot be formed with unlimited coins" is different: it's based on the concept of the Frobenius coin problem for two coins but not for arbitrary number. For more than two coins, there is no simple linear algorithm; it's NP-hard? Actually, the problem of determining the smallest unattainable sum with unlimited coins is known as the "coin problem" or "Frobenius problem" for more than two coins is hard, but there is a polynomial time algorithm if the denominations are sorted and we use DP up to some bound? But there is a greedy algorithm: If we can make all sums up to X, and we have a coin of value c <= X+1, then we can make all sums up to X+c with unlimited coins? Let's test: With unlimited coins, if we can make every sum from 0 to X, and we have a coin of denomination c (which we can use unlimited times), then we can make every sum from 0 to X+c? Not exactly. Suppose we can make any amount up to X. With unlimited c, we can make amounts: any amount that is a combination of multiples of c plus some amount <= X. The set of achievable sums is all numbers of the form k*c + y where y in [0,X] (with integer k>=0). The maximum contiguous range from 0 might extend beyond X. If c <= X+1, then the range [0, X] and [c, c+X] overlap, because c <= X+1 implies c <= X+1, so c <= X+1 => c-1 <= X => the intervals [0, X] and [c, c+X] are contiguous? Actually if c <= X+1, then the gap between X and c is at most 1? Let's compute: The intervals [0, X] and [c, c+X]. For there to be no gap, we need X+1 >= c, i.e., c <= X+1. Indeed if c <= X+1, then every integer from 0 to c+X is covered: because any number t between 0 and X is covered; any number t between X+1 and c-1? Wait, if c <= X+1, then c-1 <= X, so there is no integer strictly between X and c? Actually the intervals: [0, X] and [c, c+X]. The smallest integer not in first interval is X+1. If c <= X+1, then c <= X+1 => c <= X+1, so c could be less than or equal to X+1. If c = X+1, then second interval starts at X+1, so X+1 is covered, and the intervals join without gap. If c < X+1, then the intervals overlap because c <= X, then second interval starts at c <= X, which is already covered by first interval. So yes, if c <= X+1, then with unlimited c we can achieve all numbers up to X+c? Actually we need to ensure that we can achieve all numbers up to X+c, not just up to X+c? Let's test: with unlimited coins, if we can achieve all up to X, and we have coin c <= X+1, then we can achieve all up to X+c. Because for any number t between 0 and X+c, we can represent t as either t if t <= X, or if t > X, we can write t = c + (t-c). Since t <= X+c, t-c <= X. And we have a representation for t-c (since it's <= X). So yes, unlimited works as well! But wait, this reasoning assumes that we can use the coin c exactly once, but we have unlimited c's, so we can use it multiple times. However, if we need to use multiple c's, we need to consider representation using more than one c. But the step from X to X+c only uses one c. To extend to X+c, we need to ensure that for any t up to X+c, we can represent it using at most one c plus some amount <= X. But is that sufficient? What about numbers that require two c's? For t = X+2c, we would need two c's. But the step says we can extend from X to X+c, not to X+2c. However, after we extend to X+c, we can then apply the same reasoning again: now we know we can represent all numbers up to X+c. If we still have the same coin c (unlimited), then we can consider again: Since c <= (X+c)+1? Actually c <= X+1 <= X+c+1 trivially, so we can extend to X+2c, and so on. So indeed, with unlimited supply of a coin, we can iteratively increase the reachable range indefinitely as long as c <= current_reachable+1 initially. So the algorithm that processes each coin only once, adding its value to reachable, actually works for unlimited supply? Let's test with coins [1,3] unlimited: sorted [1,3]. reachable=0; coin 1 <=1 => reachable=1; coin 3 <=2? 3 > 2, so break, answer=2. But we argued that with unlimited 1 and 3, we can make 2 (1+1). Why did algorithm stop? Because it only added the coin value once, not accounting for multiple uses. However, if we treat unlimited supply, after we have processed coin 1, reachable becomes 1 (we can make 0 and 1). Now with unlimited 1's, we could actually make any amount because we have unlimited 1's. But in our algorithm, we only added the coin value once, but we could add it multiple times. The step that says "if c <= reachable+1, then reachable += c" is valid only if we have exactly one copy of that coin, not unlimited. For unlimited, we would need to iteratively add c as many times as needed until reachable >= something? Actually the property for unlimited coins is: if we have a set of denominations and we can make all amounts up to reachable, and we have a new denomination c, with unlimited copies, then if c <= reachable+1, we can make all amounts up to reachable + c, but then we can also use additional copies of c to extend further, but that would be captured by the fact that we still have c available; but since we only process each denomination once, we might not capture the full extent. However, the algorithm that processes each denomination once and adds its value works for the case where we have exactly one of each coin? For unlimited, we need to consider that after adding c once, reachable increases, and since we still have c (unlimited), we could add it again, but the condition c <= new_reachable+1 will still hold (since c is the same and new_reachable is larger), so we would add it again, leading to an infinite loop. So you would need to keep adding c until reachable exceeds some limit, effectively reachable becomes infinite if c=1? Actually if we have unlimited coins, the algorithm to find the smallest unattainable sum is: sort denominations; let reachable=0; for each denomination c in sorted order: if c > reachable+1 then break; else reachable += c * (maybe infinite?)? But that's not correct because for unlimited coins, after we add c, we can continue using it to increase reachable arbitrarily, as long as c is 1, but if c > 1, you might still need multiple copies to fill gaps. However, the condition c <= reachable+1 ensures that using just one copy of c extends the contiguous range to reachable+c, but to extend further, you might need to use two copies, but that's equivalent to considering that after we have extended to reachable+c, we can treat the same coin again. So we would need to consider adding c repeatedly until it no longer extends the range? But if c <= reachable+1 initially, after adding once, the new reachable is old_reachable + c. Now is c <= new_reachable+1? Since new_reachable = old_reachable + c, we have new_reachable+1 = old_reachable + c + 1. Since c >= 1, we have c <= old_reachable + c + 1 always true. So we could add c again, and again, making reachable increase without bound. That would imply that if the smallest coin is 1, we can represent all numbers (since with unlimited 1's you can). But our algorithm with one coin 1 would: reachable=0; coin 1 <=1 -> reachable=1; loop ends because no more coins; answer=2. That's wrong for unlimited. So indeed, the algorithm is for exactly one copy each. So our interpretation is correct.
Thus, problem matches subset sum with one copy each.
Now, any nuance about "coins need not be of distinct denomination" just means there can be duplicates.
Now, we must ensure that we treat duplicates correctly: The sorted array includes duplicates. The algorithm works: If we have two 1's: sorted [1,1]. Initially reachable=0; first 1 <=1 -> reachable=1; second 1 <=2 -> reachable=2; answer after processing all = 3. That's correct: with two ones, we can make 0,1,2, but not 3.
Now, everything is good.
Now, we might consider using a different approach if N is very large but we can't sort due to memory? But N is 1e5, fine.
Now, final code.
But we must include #include <bits/stdc++.h> and using namespace std; use printf/scanf.
Potential speed improvement: Use int for a_i, but when adding to reach, cast to ll.
Now, verify: In the loop, we compare a[i] > reach + 1. Since reach is ll, reach+1 is ll, a[i] is int, promotion works.
Now, I'll produce final answer.
But before finalizing, consider the possibility that the market prices start from 1 and go up to 10^17 inclusive. So if reachable >= 10^17, we output -1. As argued, impossible under constraints, but we include.
Now, test with a hypothetical: N=3, coins = [1e9, 1e9, 1e9]? Sorted: [1e9,1e9,1e9]. reachable=0; first 1e9 >1 -> answer=1. That's correct.
Now, also test with coins = [1,2,3,... up to something] sum huge? Not possible.
Now, final code.
But maybe we need to handle the case when N=0? It says 1 ≤ N, so ignore.
Now, output formatting: Each test case output one integer.
Now, let's write the code.
One more nuance: In C++, using vector with dynamic allocation for each test case might cause slowdown if T large. We could use static array: int a[100000]; but then we need to know max N. Since N can be up to 1e5 per test, we can allocate a global array of size 100000 and reuse. That might be faster. But vector is fine.
But caution: If T is 100 and each N is 1e5, constructing vector and calling sort each time might be okay. But to be safe, we can use static array and sort using pointers.
We'll implement using vector for simplicity.
Now, let's produce final answer.
We'll also add define for the limit: const long long MAXP = 100000000000000000LL;
Now, final code.
Now, we need to include the necessary headers.
I'll produce final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll LIMIT = 100000000000000000LL; // 10^17
int main() {
int T;
scanf("%d", &T);
while (T--) {
int N;
scanf("%d", &N);
vector a(N);
for (int i = 0; i < N; ++i) {
scanf("%d", &a[i]);
}
sort(a.begin(), a.end());
ll reachable = 0;
bool output_done = false;
for (int i = 0; i < N; ++i) {
// If this coin creates a gap, answer is reachable+1
if (a[i] > reachable + 1) {
printf("%lld\n", reachable + 1);
output_done = true;
break;
}
// Otherwise we can extend the covered range
reachable += a[i];
// Already can buy everything up to 10^17 ?
if (reachable >= LIMIT) {
printf("-1\n");
output_done = true;
break;
}
}
// Processed all coins without break
if (!output_done) {
if (reachable >= LIMIT) {
printf("-1\n");
} else {
printf("%lld\n", reachable + 1);
}
}
}
return 0;
}