#include <algorithm> #include <array> #include <functional> #include <iostream> using namespace std; class Series { public: bool read() { for (auto& value : values_) cin >> value; if (count(values_.begin(), values_.end(), -1) != 1) return false; return true; } void check_all() const { const int result = max( check_one(plus<int>(), minus<int>()), check_one(multiplies<int>(), divides<int>())); cout << result << '\n'; } private: array<int, 4> values_; template <typename func1, typename func2> int check_one(func1 add, func2 sub) const { auto values = values_; const int idx = find(values.begin(), values.end(), -1) - values.begin(); const int diff = (idx < 2) ? sub(values[idx + 2], values[idx + 1]) : sub(values[idx - 1], values[idx - 2]); const int result = values[idx] = (idx < 2) ? sub(values[idx + 1], diff) : add(values[idx - 1], diff); if (result <= 0 || result > 10000) return -1; for (size_t i = 1; i < values.size(); i++) { if (values[i] != add(values[i - 1], diff)) return -1; } return result; } }; int main() { static Series series; while (series.read()) { series.check_all(); } }