fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5.  
  6. #ifdef LOCAL
  7. #include "debug.h"
  8. #else
  9. #define out(...) 0
  10. #endif
  11.  
  12.  
  13. void solve() {
  14. int n, k; cin >> n >> k;
  15. vector<int> v(n);
  16.  
  17. for(int& i : v) {
  18. cin >> i;
  19. }
  20.  
  21. int ans = 1E9;
  22.  
  23. for (int i = k - 1; i < n; ++i) {
  24. if (v[i - k + 1] >= 0) ans = min(ans, v[i]);
  25. else if (v[i] <= 0) ans = min(ans, abs(v[i - k + 1]));
  26. else {
  27. ans = min(ans, abs(v[i - k + 1]) * 2 + v[i]);
  28. ans = min(ans, abs(v[i - k + 1]) + v[i] * 2);
  29. }
  30. }
  31. cout << ans << '\n';
  32. }
  33.  
  34. int main() {
  35. ios::sync_with_stdio(0);
  36. cin.tie(0);
  37.  
  38. int tc = 1;
  39. // cin >> tc;
  40.  
  41. for(int cs = 1; cs <= tc; cs++) {
  42. // cout << "Case " << cs << ": ";
  43. solve();
  44. }
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
1000000000