Source Code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll solve() {
    vector<ll> v(3);
    for (ll &i : v) cin >> i;
    sort(v.begin(), v.end());
    if (v[2] == 0) return 0;
    if (v[1] == 0) return -1;
    if (v[0] == 0 && v[1] == 1) return (v[2] == 1 ? 1 : -1);
    if (v[0] == 1 && v[2] == 1) return -1;
    if (v[0] == v[2]) return (v[2] + 1) / 2 * 3 + (v[2] & 1);
    if (v[0] + v[1] < v[2]) return v[0] + v[1] + 3 * (v[2] - v[1] - v[0]);
    ll ans = 0;
    ll diff = v[1] - v[0];
    ans += diff;
    v[1] = v[0];
    v[2] -= diff;
    diff = v[2] - v[1];
    ans += 2 * diff;
    v[0] = v[1] = v[1] - diff;
    v[2] -= 2 * diff;
    ans += (v[2] + 1) / 2 * 3 + (v[2] & 1);
    return ans;
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int t;
    cin >> t;
    while (t--) {
        cout << solve() << '\n';
    }
    return 0;
}
Copy
Apple Visits Dar Hammoudeh Heartbeat
GNU G++17
6 ms
816 KB
Accepted