-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP11111.cpp
executable file
·39 lines (36 loc) · 958 Bytes
/
P11111.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
bool ok(int &idx, vector<int> &v, int parent, int siblingSum) {
while(true) {
if(idx >= (int)v.size())
return false; // Reading out of boundary :(
int a = v[idx++];
if(a == parent)
return true;
if(a > 0)
return false; // Must start with a negative value.
siblingSum -= a; // positive sum.
if(siblingSum >= parent)
return false;
if(!ok(idx, v, -a, 0))
return false;
}
}
int main() {
string line;
while(getline(cin, line)) {
stringstream ss; ss << line;
vector<int> v;
int X;
while(ss >> X) {
v.push_back(X);
}
if(v.empty())
die(); // WTF!?
//cerr << "Running for |v|=" << v.size() << endl;
int idx = 1;
if(v[0] < 0 && ok(idx, v, -v[0], 0) && idx == (int)v.size()) // Starting with a negative int, read all OK and really did read all.
cout << ":-) Matrioshka!" << endl;
else
cout << ":-( Try again." << endl;
}
return 0;
}