Guest User

Untitled

a guest
Jan 13th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.47 KB | None | 0 0
  1.     #include<bits/stdc++.h>
  2.     #include <ext/pb_ds/tree_policy.hpp>
  3.     #include <ext/pb_ds/assoc_container.hpp>
  4.     using namespace std;
  5.     using namespace __gnu_pbds;
  6.      
  7.     #define mod 1000000007
  8.      
  9.      
  10.     /// Debugging shits start here... https://pastebin.com/K2DWYpnm
  11.      
  12.     void __print(int x) {cerr << x;}
  13.     void __print(long x) {cerr << x;}
  14.     void __print(long long x) {cerr << x;}
  15.     void __print(unsigned x) {cerr << x;}
  16.     void __print(unsigned long x) {cerr << x;}
  17.     void __print(unsigned long long x) {cerr << x;}
  18.     void __print(float x) {cerr << x;}
  19.     void __print(double x) {cerr << x;}
  20.     void __print(long double x) {cerr << x;}
  21.     void __print(char x) {cerr << '\'' << x << '\'';}
  22.     void __print(const char *x) {cerr << '\"' << x << '\"';}
  23.     void __print(const string &x) {cerr << '\"' << x << '\"';}
  24.     void __print(bool x) {cerr << (x ? "true" : "false");}
  25.      
  26.     template<typename T, typename V>
  27.     void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
  28.     template<typename T>
  29.     void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
  30.     void _print() {cerr << "]\n";}
  31.     template <typename T, typename... V>
  32.     void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
  33.     #ifndef ONLINE_JUDGE
  34.     #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
  35.     #else
  36.     #define debug(x...)
  37.     #endif
  38.     /// Debugging shits ends here...
  39.     // templates
  40.     /*#include<ext/pb_ds/assoc_container.hpp>
  41.     #include<ext/pb_ds/tree_policy.hpp>
  42.     using namespace __gnu_pbds;
  43.     /*template <typename T>
  44.     using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;*/
  45.      
  46.     /// Some frequent usable functions
  47.     #define int                        long long
  48.     void add(int &a,int b){a+=b;if(a>mod)a-=mod;}
  49.     void sub(int &a,int b){a-=b;if(a<0)a+=mod;}
  50.     void mul(int &a, int b) {a=1ll * a * b % mod;}
  51.     template<typename T> T pow(T a,T b, long long m){T ans=1; while(b>0){ if(b%2==1) ans=(ans*a)%m; b/=2; a=(a*a)%m; } return ans%m; }
  52.     int powmod(int a,int b)
  53.     {int res = 1;while(b){if(b&1){res = (res * a)%mod;}b= b/2;a = (a*a)%mod;}return res;}
  54.      
  55.     int gcd(int a, int b, int &x, int &y) {if (a == 0) {x = 0; y = 1;return b;
  56.     }int x1, y1;int d = gcd(b%a, a, x1, y1); x = y1 - (b / a) * x1;y = x1;return d;}
  57.     //int find(int v){return v==parent[v]?v:parent[v] = find(parent[v]);}
  58.     // void merge(int i,int j)
  59.     // {i = find(i);j = find(j);if(i == j)return;parent[parent[i]] = parent[j];cmp--;}
  60.      
  61.     // Directions.......
  62.     /* int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; */ // 4 Direction
  63.     /* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
  64.     /* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight moves
  65.     /* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction
  66.     #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  67.     #define sp(a , x) cout << fixed << setprecision(a) << x << endl;
  68.     #define endl "\n"
  69.     #define pb push_back
  70.     #define pf push_front
  71.     #define ub upper_bound
  72.     #define lb lower_bound
  73.     #define F first
  74.     #define S second
  75.     #define mset(a, b) memset(a, b, sizeof a)
  76.     #define sz(x) ((ll)(x.size()))
  77.     #define sqr(x) ((x) * (x))
  78.     #define graph vector<int>
  79.     #define vi vector<int>
  80.     #define vvi vector<vector<int>>
  81.     #define pi pair<int,int>
  82.     #define all(c)                      c.begin() , c.end()
  83.     #define rep(i,a,b) for(int i=a;i<=b;i++)
  84.     #define rrep(i,a,b) for(int i=a;i>=b;i--)
  85.     #define iter(it,a) for(auto it=a.begin();it!=a.end();it++)
  86.     #define PQP priority_queue<pi, vector<pi>, greater<pi>>
  87.     #define PQI priority_queue<int, vector<int>, greater<int>>
  88.     #define dbg debug
  89.     #define inf (int)1e16
  90.     const long double EPS = 0.0000000001;
  91.     const long double PI = (acos(-1));
  92.      
  93.      
  94.     /// define some data.......
  95.      
  96.   const int N = (int)2e6+5;
  97.   int n,k;
  98.   int a[N];
  99.   int dp[N];
  100.    void solve()
  101.  {
  102.    cin >> n >> k;
  103.    
  104.    rep(i,1,n)
  105.    {
  106.      cin >> a[i];
  107.      dp[i] = a[i];
  108.    }
  109.    
  110.    int no_of_blocks = (n + k -1)/k;
  111.    
  112.    for(int i=2;i<=no_of_blocks;i++)
  113.    {
  114.      int l = max(1ll , ((i-2)*k) + 1);
  115.      int r = min(n , (i-1)*k);
  116.      int mx_1 , mx_2;
  117.      mx_1 = mx_2 = -inf;
  118.      int id1 , id2 = 0;
  119.      for(int j=l;j<=r;j++)
  120.      {
  121.        if(dp[j] >= mx_1)
  122.        {
  123.          mx_2 = mx_1;
  124.          id2 = id1;
  125.          mx_1 = dp[j];
  126.          id1 = j;
  127.        }
  128.        else if(dp[j] > mx_2)
  129.        {
  130.          mx_2 = dp[j];
  131.          id2 = j;
  132.        }
  133.      }
  134.      
  135.      l = ((i-1)*k)+1 , r = min(i*k,n);
  136.      
  137.      for(int j = l;j<=r;j++ )
  138.      {
  139.        dp[j] = max(dp[j] , dp[max(0ll , j - 2*k)]+a[j]);
  140.        
  141.        if((j-id1) != k)
  142.        {
  143.          dp[j] = max(dp[j] , mx_1 + a[j]);
  144.        }
  145.        
  146.        if((j - id2) != k)
  147.        {
  148.          dp[j] = max(dp[j] , mx_2 + a[j]);
  149.        }
  150.      }
  151.    }
  152.      
  153.      int ans = 0;
  154.      rep(i,1,n)
  155.      {
  156.        ans = max(ans , dp[i]);
  157.      }
  158.      
  159.      cout<<ans<<"\n";
  160.  }
  161.        
  162.     int32_t main(){
  163.          fast;
  164.       int test=1;
  165.       //cin >> test;
  166.        int t=1;  
  167.        while(test--)
  168.        {
  169.          solve();
  170.          mset(a,0);
  171.          mset(dp,0);  
  172.        }
  173.      
  174.     }
Add Comment
Please, Sign In to add comment