fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ms(s,n) memset(s,n,sizeof(s))
  5. #define all(a) a.begin(),a.end()
  6. #define present(t, x) (t.find(x) != t.end())
  7. #define sz(a) int((a).size())
  8. #define FOR(i, a, b) for (int i = (a); i < (b); ++i)
  9. #define FORd(i, a, b) for (int i = (a) - 1; i >= (b); --i)
  10. #define pb push_back
  11. #define pf push_front
  12. #define fi first
  13. #define se second
  14. #define mp make_pair
  15.  
  16. typedef long long ll;
  17. typedef unsigned long long ull;
  18. typedef long double ld;
  19. typedef pair<int,int> pi;
  20. typedef vector<int> vi;
  21. typedef vector<pi> vii;
  22.  
  23. const int MOD = (int) 1e9+7;
  24. const int INF = (int) 1e9+1;
  25. inline ll gcd(ll a,ll b){ll r;while(b){r=a%b;a=b;b=r;}return a;}
  26. inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
  27.  
  28. bool cmp(pi a, pi b){
  29. if(abs(a.fi) != abs(b.fi))
  30. return abs(a.fi) < abs(b.fi);
  31. return a.se < b.se;
  32. }
  33.  
  34. #define endl "\n"
  35.  
  36. void TC(){
  37. int n; cin >> n;
  38. vii a(n);
  39. for(int i = 0; i < n; i++){
  40. int x; cin >> x;
  41. a[i].first = x;
  42. a[i].se = i;
  43. }
  44. sort(all(a), cmp);
  45. ll res, sum = INT_MAX;
  46. int idx;
  47. for(int i = 1; i < n; i++){
  48. int tmp = abs(a[i].fi + a[i-1].fi);
  49. if(tmp < sum){
  50. sum = tmp;
  51. res = a[i].fi + a[i-1].fi;
  52. idx = min(a[i].se, a[i-1].se);
  53. }
  54. else if(tmp==sum){
  55. if(idx > min(a[i].se, a[i-1].se)){
  56. res = a[i].fi + a[i-1].fi;
  57. idx = min(a[i].se, a[i-1].se);
  58. }
  59. }
  60. }
  61. cout << res << endl;
  62.  
  63. }
  64.  
  65. int main(){
  66. //freopen("input.txt","r", stdin);
  67. //freopen("output.txt","w",stdout);
  68. ios::sync_with_stdio(false);
  69. cin.tie(nullptr);
  70. int t; cin >> t;
  71. while(t--){
  72. TC();
  73. }
  74. }
Runtime error #stdin #stdout 0.68s 2095808KB
stdin
Standard input is empty
stdout
Standard output is empty