fork download
  1. /*
  2. ID: kadir_k2
  3. PROG: test
  4. LANG: C++
  5. */
  6.  
  7. #include <bits/stdc++.h>
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11. #include <cmath>
  12. #include <iomanip>
  13. #include <deque>
  14. #include <vector>
  15. #include <algorithm>
  16. #include <time.h>
  17. #include <map>
  18. #include <set>
  19. #include <string>
  20.  
  21. using namespace std;
  22.  
  23. #define IF ifstream cin("input.txt")
  24. #define OF ofstream cout("output.txt")
  25. #define rep(st,i,n) for(i = st; n > i ; i ++ )
  26. #define pb push_back
  27. #define fi first
  28. #define se second
  29. #define ins insert
  30. #define make make_pair
  31. #define NAME "seq"
  32. typedef long long ll;
  33. const double PI = 3.1415926535897932384626433832795;
  34. int a[ 200009 ];
  35. long long s[ 200009 ];
  36. long long P ;
  37. long long all ;
  38. const long long inf = (ll)1e18;
  39. int main(){
  40. freopen("input.txt" , "r" ,stdin);
  41. freopen("output.txt" , "w" ,stdout);
  42. int i , n , h;
  43. cin >> n >> h;
  44. for(i = 0; n > i ; i++){
  45. cin >> a[i];
  46. s[i] = a[i];
  47. if(i)s[i] += s[i-1];
  48. }
  49. all = ( h * 1LL * (h + 1) ) / (2*1LL);
  50. int T = h;
  51. long long ans = inf;
  52. for(i = n-1; i>=-1 ; i--)
  53. {
  54. if(!T){
  55. if(i!=-1)ans = min(ans , all - (s[i+h] - s[i]) );
  56. else ans = min(ans , all - (s[i+h]) );
  57. T = 1;
  58. }
  59. if(i ==-1);
  60. else if( a[i] <= T )T -- ;
  61. else if( a[i] <= h)T = a[i] - 1;
  62. else T = h;
  63. }
  64. if(ans == inf)cout << -1 << endl;
  65. else cout << ans << endl;
  66. return 0;
  67. }
Success #stdin #stdout 0s 5680KB
stdin
Standard input is empty
stdout
Standard output is empty