fork download
  1. /*input
  2. 2
  3. 1 1
  4. 2 2
  5. */
  6. #pragma GCC optimize ("O3")
  7. #include <bits/stdc++.h>
  8. #include <ext/pb_ds/assoc_container.hpp>
  9. using namespace std;
  10. using namespace __gnu_pbds;
  11. typedef long long ll;
  12. typedef long double ld;
  13. int main()
  14. {
  15. clock_t pradzia = clock();
  16. ios_base::sync_with_stdio(false);
  17. int n;
  18. cin >> n;
  19. ll a[n], b[n];
  20. for (int i = 0; i < n; i++)
  21. cin >> a[i] >> b[i];
  22. while ((clock() - pradzia) * 10 <= 9 * CLOCKS_PER_SEC)
  23. {
  24. bool ok = true;
  25. for (int i = n - 1; i >= 0; i--)
  26. {
  27. if (a[i] < b[i])
  28. {
  29. ll c = min(a[(i - 1 + n) % n] / 2, b[i] - a[i]);
  30. a[i] += c;
  31. a[(i - 1 + n) % n] -= 2 * c;
  32. ok = false;
  33. }
  34. }
  35. for (int i = 0; i < n; i++)
  36. {
  37. if (a[i] > b[i])
  38. {
  39. ll c = min(a[i] / 2, (a[i] - b[i] + 1) / 2);
  40. a[i] -= c * 2;
  41. a[(i + 1) % n] += c;
  42. ok = false;
  43. }
  44. }
  45. if (ok)
  46. {
  47. cout << "Yes\n";
  48. return 0;
  49. }
  50. }
  51. cout << "No\n";
  52. }
Success #stdin #stdout 0s 15232KB
stdin
2
1 1
2 2
stdout
Yes