fork download
  1. // OM NAMAH SHIVAY
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. void solve(){
  6. int n,k;
  7. cin >> n >> k ;
  8. vector<int> v(n) ;
  9. for(int i=0 ;i<n ;i++){
  10. cin >> v[i];
  11. }
  12.  
  13. unordered_map<int,long long> hashMap;
  14. vector<long long> pref(n,0);
  15. pref[0]=v[0];
  16. for(int i=1 ;i<n ;i++){
  17. pref[i]=pref[i-1]+v[i];
  18. }
  19. long long ans=LLONG_MIN;
  20. for(int i=0;i<n ;i++){
  21. if(hashMap.find(v[i])==hashMap.end())
  22. hashMap[v[i]]=pref[i]-v[i];
  23. else
  24. hashMap[v[i]]=min(hashMap[v[i]],pref[i]-v[i]);
  25.  
  26. if(hashMap.find(v[i]-k)!=hashMap.end()){
  27. ans=max(ans,pref[i]-hashMap[v[i]-k]);
  28. }
  29. if(hashMap.find(v[i]+k)!=hashMap.end()){
  30. ans=max(ans,pref[i]-hashMap[v[i]+k]);
  31. }
  32.  
  33.  
  34. }
  35. if(ans==LLONG_MIN){
  36. cout << "IMPOSSIBLE\n";
  37. }
  38. else cout << ans << "\n" ;
  39. }
  40. int main() {
  41. // your code goes here
  42. int testCases;
  43. cin >> testCases;
  44. while(testCases--) solve();
  45. return 0;
  46. }
Success #stdin #stdout 0s 5320KB
stdin
1
4 5
1 2 3 4
stdout
IMPOSSIBLE