Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded


// Problem :
// 			D - Moving Piece
//
//
// Contest : AtCoder - AtCoder Beginner Contest 175
// URL : https://atcoder.jp/contests/abc175/tasks/abc175_d
// Memory Limit : 1024 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define db double
#define mod 1000000007
#define pb push_back
#define pp pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repe(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = b; i >= a; i--)
#define maxn 200005
#define ss second
#define ff first
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
int inv(int a, int b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; }
int fun(vector<int> &v, int k) {
  int end = v.size() - 1, n = v.size();
  int mp[n + 1];
  for (int l = 1; l <= n; l++) {
    int curr = 0;
    for (int i = 0; i < end + l; i++) {
      curr += v[i % n];
      if (i >= l)
        curr -= (v[i - l]);
      if (i >= l - 1) {
        if (i == l - 1)
          mp[l] = curr;
        else
          mp[l] = max(mp[l], curr);
        // cout << mp[l] <<"\\n";
      }
    }
  }
  int ans;
  if (mp[n] > 0) {
    int q = k / n;
    int r = k % n;
    int curr = 0, tmp = q * mp[n], rem = 0;
    for (int i = 1; i <= r; i++)
      rem = max(rem, mp[i]);
    tmp += rem;
    if (q > 1) {
      curr = (q - 1) * mp[n];
      int rem = 0;
      for (int i = 1; i <= n; i++)
        rem = max(rem, mp[i]);
      curr += rem;
    }
    tmp = max(tmp, curr);
    ans = tmp;
  } else {
    ans = mp[1];
    for (int i = 1; i <= n; i++) {
      // cout << mp[i] <<"\\n";
      ans = max(ans, mp[i]);
    }
  }
  return ans;
}
void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n), c(n), vis(n, 0);
  for (auto &x : p) {
    cin >> x;
    x--;
  }
  for (auto &x : c)
    cin >> x;
  int ans = -mod;
  for (int i = 0; i < n; i++) {
    if (vis[i] == 0) {
      vector<int> tmp;
      int st = p[i];
      tmp.pb(c[i]);
      vis[i] = 1;
      while (st != i) {
        tmp.pb(c[st]);
        vis[st] = 1;
        st = p[st];
      }
      ans = max(ans, fun(tmp, k));
    }
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}


Error: TLE

Fixed submission:


// Problem :
// 			D - Moving Piece
//
//
// Contest : AtCoder - AtCoder Beginner Contest 175
// URL : https://atcoder.jp/contests/abc175/tasks/abc175_d
// Memory Limit : 1024 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define db double
#define mod 1000000007
#define pb push_back
#define pp pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repe(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = b; i >= a; i--)
#define maxn 200005
#define ss second
#define ff first
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
int inv(int a, int b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; }
int fun(vector<int> &v, int k) {
  map<int, int> mp;
  int end = v.size() - 1, n = v.size();
  for (int l = 1; l <= n; l++) {
    int curr = 0;
    for (int i = 0; i < end + l; i++) {
      curr += v[i % n];
      if (i >= l)
        curr -= (v[i - l]);
      if (i >= l - 1) {
        if (mp.count(l) == 0)
          mp[l] = curr;
        else
          mp[l] = max(mp[l], curr);
        // cout << curr <<" ";
      }
    }
    // cout <<"\\n";
  }
  int ans = -mod;
  if (mp[n] > 0) {
    int q = k / n;
    int r = k % n;
    int curr = 0, tmp = q * mp[n], rem = 0;
    for (int i = 1; i <= r; i++)
      rem = max(rem, mp[i]);
    tmp += rem;
    if (q > 1) {
      curr = (q - 1) * mp[n];
      int rem = 0;
      for (int i = 1; i <= n; i++)
        rem = max(rem, mp[i]);
      curr += rem;
    }
    tmp = max(tmp, curr);
    ans = max(ans, tmp);
  } else {
    for (int i = 1; i <= k; i++)
      ans = max(ans, mp[i]);
  }
  return ans;
}
void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n), c(n), vis(n, 0);
  for (auto &x : p) {
    cin >> x;
    x--;
  }
  for (auto &x : c)
    cin >> x;
  int ans = -mod;
  for (int i = 0; i < n; i++) {
    if (vis[i] == 0) {
      vector<int> tmp;
      int st = p[i];
      tmp.pb(c[i]);
      vis[i] = 1;
      while (st != i) {
        tmp.pb(c[st]);
        vis[st] = 1;
        st = p[st];
      }
      ans = max(ans, fun(tmp, k));
    }
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}