fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3.  
  4. using namespace std;
  5.  
  6. const int MOD = 1e9 + 7;
  7.  
  8.  
  9. void solve(){
  10. ll n, m, r1, r2;
  11. cin >> n >> m >> r1 >> r2;
  12.  
  13. if(n > m)swap(n, m);
  14. if(r1 > r2)swap(r1, r2);
  15.  
  16. ll g, h, diff;
  17.  
  18. g = 2 * r1 + 1 + 1 + 2 * r1 + 1 + (r2 - r1) + (r2 == r1);
  19. g = max(g, 2 * r2 + 1);
  20. h = 2 * r2 + 1 + (r1 == r2);
  21.  
  22. if(n - h > 0){
  23. if(g - (2 *r2 + 1) > 0){
  24. diff = min(n - h, g - (2 * r2 + 1));
  25. h += diff;
  26. g -= diff;
  27. }
  28. }
  29. if(g <= m && h <= n){
  30. cout << 2 * r1 + 2 + h - r2 + g - r2 << "\n";
  31. }else{
  32. cout << -1 << "\n";
  33. }
  34.  
  35.  
  36.  
  37.  
  38. }
  39.  
  40. int main(){
  41. ios_base::sync_with_stdio(false);
  42. cin.tie(NULL);
  43.  
  44. // cout << fixed << setprecision(7);
  45. int t = 1;
  46. cin >> t;
  47. for(int i = 1; i <= t; i++){
  48. // cout << "Case #" << i << ": ";
  49. solve();
  50. }
  51. return 0;
  52. }
Success #stdin #stdout 0s 5284KB
stdin
3
7 13 2 3
20 20 2 9
5 4 1 1
stdout
19
26
-1