alexjercan/bugnet huggingface dataset:
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long

int32_t main() {
  int x = 6, k = 2, d = 4;
  ;
  cin >> x >> k >> d;
  //  int min = 1000000000000000;
  // cin>>x;
  // char sw = (x>0)?'p':'n';
  int fact, rem;
  // while(true){
  //     if(x>0){
  //         x-=d;
  //         // cout<<"d";
  //         if(x<0&&sw=='p'){
  //             // cout<<"1";
  //             if(k%2==0){
  //                 cout<<abs(x);
  //                 return 0;
  //             }else{
  //                 cout<<abs(x+d);
  //                 return 0 ;
  //             }
  //         }
  //         if(x<0){
  //             sw ='n';
  //         }

  //     }else{
  //         x+=d;
  //         // cout<<"in";
  //         if(x>0&&sw=='n'){
  //             // cout<<"2";
  //             if(k%2==0){
  //                 cout<<abs(x);
  //                 return 0 ;
  //             }else{

  //                 cout<<abs(x-d);
  //                 return 0 ;
  //             }
  //         }
  //         if(x>0){
  //             sw='p';
  //         }
  //     }
  //     if(x==0){
  //          if(k%2==0){
  //                 cout<<abs(x);
  //                 return 0 ;
  //             }else{

  //                 cout<<abs(x-d);
  //                 return 0 ;
  //             }
  //     }

  //     fact = abs(x/d);
  //     k-=fact;
  //     x = (x>0)? x- fact*d : x + fact*d ;
  //     if(k>0){

  //     }else{
  //         break;
  //     }

  // }
  fact = (x / d);

  if (abs(fact) > k) {
    if (x > 0) {
      cout << abs(x - k * d);
    } else {
      cout << abs(x + k * d);
    }

    return 0;
  }
  x = x - fact * d;
  fact = abs(fact);
  k -= fact;
  // cout<<" k remain "<<k<<" x is :"<<x;
  if (k % 2 == 0) {
    cout << abs(x);
    return 0;
  } else {
    if (x > 0) {
      cout << abs(x - d);
    } else {
      cout << abs(x + d);
    }
  }
  // cout<<"k is :"<<k<<" ";
  // cout<<abs(x);

  return 0;
}

// x  = -7   d = 2    k =3

// fact = x/d = 3 ;

// k-=fact ;

// x= x- fact*d;

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long

int32_t main() {
  int x = 6, k = 2, d = 4;
  ;
  cin >> x >> k >> d;
  int min = 1000000000000000;
  // cin>>x;
  char sw = (x > 0) ? 'p' : 'n';
  while (k--) {
    if (x > 0) {
      x -= d;
      // cout<<"d";
      if (x < 0 && sw == 'p') {
        // cout<<"1";
        if (k % 2 == 0) {
          cout << abs(x);
          return 0;
        } else {
          cout << abs(x + d);
          return 0;
        }
      }
      if (x < 0) {
        sw = 'n';
      }

    } else {
      x += d;
      // cout<<"in";
      if (x > 0 && sw == 'n') {
        // cout<<"2";
        if (k % 2 == 0) {
          cout << abs(x);
          return 0;
        } else {

          cout << abs(x - d);
          return 0;
        }
      }
      if (x > 0) {
        sw = 'p';
      }
    }
    if (x == 0) {
      if (k % 2 == 0) {
        cout << abs(x);
        return 0;
      } else {

        cout << abs(x - d);
        return 0;
      }
    }
  }
  cout << abs(x);

  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {
  long long N, K, D;
  long long shouryaku;
  cin >> N >> K >> D;

  if (N < 0) {
    N = -N;
  }
  shouryaku = N / D;

  if (shouryaku < K) {
    N = N - (shouryaku * D);
    K = K - shouryaku;
    if (K % 2 == 0) {
      cout << N << endl;
    } else if (K % 2 == 1) {
      cout << -(N - D) << endl;
    }
  } else {
    N = N - K * D;
    cout << N << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  long long N, K, D, i;
  cin >> N >> K >> D;

  if (N < 0) {
    N = -N;
  }

  for (i = 1; i <= K; i++) {
    N -= D;
    if (N < 0) {
      break;
    }
  }
  if (K < i) {
    i--;
  }
  if ((K - i) % 2 == 0) {
    if (N < 0) {
      N = -N;
    }
    cout << N << endl;
  } else if ((K - i) % 2 == 1) {
    cout << N + D << endl;
  }
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long X, K, D, ans;
  long long amari;
  scanf(" %lld %lld %lld", &X, &K, &D);
  X = abs(X);
  amari = floor(X / D);
  if (amari >= K)
    ans = X - K * D;
  else if ((K - amari) % 2 == 0)
    ans = abs(X - amari * D);
  else
    ans = abs(X - (amari + 1) * D);
  printf("%lld\\n", ans);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  long long X;
  long long K;
  long long D;
  scanf(" %lld %lld %lld", &X, &K, &D);
  long long ans = abs(X);
  int guuki = K % 2;
  int i = 0;
  int hoge = 0;
  long long amari = X / D;
  if (amari <= K) {
    ans -= D * amari;
    i = amari;
  } else {
    ans -= D * K;
    hoge = 1;
  }
  for (; i < K && hoge == 0; i++) {
    if (abs(ans - D) >= abs(ans)) {
      if (i % 2 != guuki)
        ans = abs(ans - D);
      break;
    }
    ans = abs(ans - D);
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using PP = pair<ll, pair<ll, ll>>;
#define rep(i, n) for (ll i = 0; i < (n); ++i)

ll x, k, d, ans;

int main() {
  cin >> x >> k >> d;

  x = abs(x);

  if ((ll)x / d < k) {
    if ((k - (ll)x / d) % 2) {
      ans = d - (x % d);
    } else {
      ans = x % d;
    }
  } else {
    if ((ll)x / d == k) {
      ans = x % d;
    } else {
      ans = x - (k * d);
    }
  }

  ans = abs(ans);

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using PP = pair<ll, pair<ll, ll>>;
#define rep(i, n) for (ll i = 0; i < (n); ++i)

ll x, k, d, ans;

int main() {
  cin >> x >> k >> d;

  x = abs(x);

  if ((ll)x / d < k) {
    if ((k - (ll)x / d) % 2) {
      ans = d - (x % d);
    } else {
      ans = x % d;
    }
  } else {
    if ((ll)x / d == k) {
      rep(i, d) {
        if (x % d == i) {
          ans = i;
        }
      }
    } else {
      ans = x - (k * d);
    }
  }

  ans = abs(ans);

  cout << ans << endl;

  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const long long INF = 1LL << 60;
#define PI 3.14159265359

template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

long long lcm(int a, int b) { return (long long)a * b / gcd(a, b); }

long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; }

int main() {
  long long X, K, D;
  cin >> X >> K >> D;

  long long x = abs(X);

  K -= x / D;
  x = x % D;

  if (K <= 0) {
    cout << x - D * K << endl;
    return 0;
  }
  if (K > 0 && K % 2 == 1) {
    x = x - D;
  }

  cout << abs(x) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const long long INF = 1LL << 60;
#define PI 3.14159265359

template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

long long lcm(int a, int b) { return (long long)a * b / gcd(a, b); }

long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; }

int main() {
  long long X, K, D;
  cin >> X >> K >> D;

  long long x = abs(X);

  while (x > 0 && K > 0) {
    x -= D;
    K--;
  }

  if (K > 0 && K % 2 == 1) {
    x = x + D;
  }

  cout << abs(x) << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long x, k, d, s;
  cin >> x >> k >> d;
  x = abs(x);
  s = min(k, x / d);
  if ((k - s) % 2 == 0) {
    cout << x - s * d << endl;
  } else {
    cout << (1 + s) * d - x << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long x, k, d;
  cin >> x >> k >> d;
  while (k) {
    if (0 <= x) {
      x -= d;
    } else {
      x += d;
    }
    k--;
  }
  if (x < 0)
    x = x * (-1);
  cout << x << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {

  long long x, k, d;
  cin >> x >> k >> d;
  long long abso = abs(x);
  long long count = 0;
  if (abso <= d) {
    if (k % 2 == 1) {
      cout << abs(abso - d);
    } else {
      cout << abso;
    }
  } else {
    long long dist = abso - d;
    long long steps = dist % d == 0 ? dist / d : dist / d + 1;
    if (steps >= k) {
      cout << abso - k * d;
    } else {
      long long curpos = abso - steps * d;
      long long rem = k - steps;
      if (rem % 2 == 1) {
        cout << abs(curpos - d);
      } else {
        cout << curpos;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {

  long long x, k, d;
  cin >> x >> k >> d;
  long long abso = abs(x);
  long long count = 0;
  while (abso >= d && count < k) {
    abso -= d;
    count++;
  }

  if (count == k) {
    cout << abso;
  } else {
    long long rem = k - count;
    if (rem % 2) {
      cout << abs(abso - d);
    } else {
      cout << abso;
    }
  }

  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

ll simu(ll x, ll k, ll d) {
  if (k == 0) {
    // cout<<x<<endl;
    return x;
  }
  if (x > 0) {
    if (x - d < 0) {
      if (k % 2 == 0)
        return x;
      else
        return x - d;
    }
    // cout<<x<<" "<<k<<" "<<endl;
    ll n = min(x / d, k);
    return simu(x - d * n, k - n, d);
  } else {
    if (x + d > 0) {
      if (k % 2 == 0)
        return x;
      else
        return x + d;
    }
    ll n = min(abs(x) / d, k);
    return simu(x + d * n, k - n, d);
  }
}

int main() {
  ll X, K, D;
  cin >> X >> K >> D;
  cout << abs(simu(X, K, D));
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

ll simu(ll x, ll k, ll d) {
  if (k == 0) {
    // cout<<x<<endl;
    return x;
  }
  if (x > 0) {
    if (x - d < 0) {
      if (k % 2 == 0)
        return x;
      else
        return x - d;
    }
    // cout<<x<<" "<<k<<" "<<endl;
    return simu(x - d, k - 1, d);
  } else {
    if (x + d > 0) {
      if (k % 2 == 0)
        return x;
      else
        return x + d;
    }
    return simu(x + d, k - 1, d);
  }
}

int main() {
  ll X, K, D;
  cin >> X >> K >> D;
  cout << abs(simu(X, K, D));
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 987654321
#define INT_MAX 2147483647
#define MIN(a, b) ((a) < (b) ? a : b)
#define MAX(a, b) ((a) > (b) ? a : b)

using namespace std;

int main() {
  long int X, K, D;
  cin >> X >> K >> D;
  X = abs(X);

  if (K <= X / D) {
    cout << X - K * D;
  } else {
    int i = X / D;
    if ((K - i) % 2 == 0)
      cout << X - i * D;
    else
      cout << abs(X - i * D - D);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 987654321
#define INT_MAX 2147483647
#define MIN(a, b) ((a) < (b) ? a : b)
#define MAX(a, b) ((a) > (b) ? a : b)

using namespace std;

int main() {
  long int X, K, D;
  cin >> X >> K >> D;

  if (X < 0) {
    long int temp = X;
    int i = 1;
    while (1) {
      temp += D;
      if (temp >= 0)
        break;
      if (i == K)
        break;
      i++;
    }
    if ((K - i) % 2 == 0) {
      cout << abs(temp);
    } else
      cout << abs(temp - D);
  }

  if (X >= 0) {
    long int temp = X;
    int i = 1;
    while (1) {
      temp -= D;
      if (temp < 0)
        break;
      if (i == K)
        break;
      i++;
    }
    if ((K - i) % 2 == 0) {
      cout << abs(temp);
    } else
      cout << abs(temp + D);
  }
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  ll a = abs(x) / d;
  ll ans;
  if (a == k)
    ans = abs(x) - d * a;
  else if (a < k) {
    k -= a;
    if (k & 1)
      ans = d * (a + 1) - abs(x);
    else
      ans = abs(x) - d * a;
  } else
    ans = abs(x) - d * k;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
int main() {
  ll x, k, d;
  scanf("%lld%lld%lld", &x, &k, &d);
  if (x == d || -x == d) {
    printf("%lld\\n", (k & 1 ? 0 : d));
    return 0;
  }
  if (k == 1) {
    printf("%lld\\n", (x >= 0 ? abs(x - d) : abs(x + d)));
    return 0;
  }
  if (x >= 0) {
    while (x - d >= 0) {
      x -= d;
      k--;
      if (!k) {
        printf("%lld\\n", abs(x));
        return 0;
      }
    }
  }
  if (x < 0) {
    while (x + d < 0) {
      x += d;
      k--;
      if (!k) {
        printf("%lld\\n", abs(x));
        return 0;
      }
    }
  }
  if (k & 1) {
    printf("%lld\\n", (abs(x - d) >= abs(x + d) ? abs(x + d) : abs(x - d)));
    return 0;
  } else {
    printf("%lld\\n", abs(x));
    return 0;
  }
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <cmath>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int x, k, d;
  cin >> x >> k >> d;
  double c = 0;
  if (x >= 0) {
    double n = -1 - x;
    c = ceil(abs(n / d));
    long long int l;
    if (c <= k) {
      l = k - c;
      x = x - (c * d);
      long long int r = x + d;
      if (l % 2 == 0)
        cout << abs(x);
      else
        cout << abs(r);
    } else {
      x = x - (k * d);
      cout << abs(x);
    }
  } else {
    double n = 1 + x;
    c = ceil(abs(n / d));
    long long int l;
    if (c <= k) {
      l = k - c;
      x = x + (c * d);
      long long int r = x - d;
      if (l % 2 == 0)
        cout << abs(x);
      else
        cout << abs(r);
    } else {
      x = x + (k * d);
      cout << abs(x);
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int x, k, d;
  cin >> x >> k >> d;
  long long int c = 0;
  if (x >= 0) {
    while (c < k && x >= 0) {
      x = x - d;
      c++;
    }
    long long int l = k - c;
    long long int r = x + d;
    if (l % 2 == 0)
      cout << abs(x);
    else
      cout << abs(r);
  } else {
    while (c < k && x < 0) {
      x = x + d;
      c++;
    }
    long long int l = k - c;
    long long int r = x - d;
    if (l % 2 == 0)
      cout << abs(x);
    else
      cout << abs(r);
  }
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 1000000007

int binpow(int a, int b) {
  int ans = 1;
  if (b == 0)
    return 1;
  while (b) {
    if (b & 1)
      ans = ans * a;
    a = a * a;
    b >>= 1;
  }
  return ans;
}

// read question carefully!

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    int x, k, d;
    cin >> x >> k >> d;
    x = abs(x);
    int step = x / d;
    if ((step & 1) != (k & 1)) {
      step--;
    }
    step = min(step, k);
    int g1 = x - (d * step);
    int g2 = x - (d * min(k, step + 2));
    int ans = min(abs(g1), abs(g2));
    cout << ans << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 1000000007

int binpow(int a, int b) {
  int ans = 1;
  if (b == 0)
    return 1;
  while (b) {
    if (b & 1)
      ans = ans * a;
    a = a * a;
    b >>= 1;
  }
  return ans;
}

// read question carefully!

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    int x, k, d;
    cin >> x >> k >> d;
    int temp = x;
    if (x >= 0) {
      int ct = 0;
      bool f = 0;
      int k1 = k;
      while (k1--) {
        if (temp <= 0) {
          f = 1;
          break;
        }
        temp = temp - d;
        ct++;
      }
      if (f) {
        int rem = k - ct;
        if (rem % 2) {
          temp += d;
        }
        cout << abs(temp) << "\\n";
      } else {
        cout << abs(temp) << "\\n";
      }
    } else {
      int ct = 0;
      bool f = 0;
      int k1 = k;
      while (k1--) {
        if (temp >= 0) {
          f = 1;
          break;
        }
        temp = temp + d;
        ct++;
      }
      if (f) {
        int rem = k - ct;
        if (rem % 2) {
          temp -= d;
        }
        cout << abs(temp) << "\\n";
      } else {
        cout << abs(temp) << "\\n";
      }
    }
  }
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;

ll minvalue(ll x, ll y) {
  if (x < y)
    return x;
  return y;
}

int main() {
  std::ios::sync_with_stdio(false);
  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  if ((x / d) > k) {
    cout << x - (k * d) << "\\n";
    return 0;
  }
  ll ans1 = x - (x / d) * d;
  ll ans2 = abs(ans1 - d);
  // cout<<ans1<<" ";
  if ((k - x / d) % 2 == 0)
    cout << ans1 << "\\n";
  else
    cout << ans2 << "\\n";
  return 0;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;

ll minvalue(ll x, ll y) {
  if (x < y)
    return x;
  return y;
}

int main() {
  std::ios::sync_with_stdio(false);
  ll x, k, d;
  cin >> x >> k >> d;
  if (x < 0)
    x = -x;
  ll min = x, pos = x, i;
  for (i = 1; i <= k; i++) {
    if (abs(pos - d) < min) {
      min = abs(pos - d);
      pos = pos - d;
    } else
      break;
  }
  k = k - i + 1;
  k = k % 2;
  if (k) {
    cout << minvalue(abs(pos - d), abs(pos + d));
  } else
    cout << min;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a) for (int i = 0; i < (a); i++)

typedef pair<int, int> P;
typedef long long ll;

const int INF = 1001001001;
const int MOD = 1000000007;
const double PI = acos(-1);

int main() {
  int N;
  ll K;
  cin >> N >> K;
  ll P[N], C[N];
  rep(i, N) {
    ll p;
    cin >> p;
    p--;
    P[i] = p;
  }
  rep(i, N) {
    ll c;
    cin >> c;
    C[i] = c;
  }
  ll ans = -1e18;

  rep(i, N) {
    // i = 1;
    set<ll> seen;
    vector<ll> A;
    ll psum = 0;
    ll s = i;
    ll c = 0;

    while (1) {
      A.push_back(C[P[s]]);
      psum += C[P[s]];
      c++;
      s = P[s];
      if (s == i)
        break;
    }

    ll tmp = 0;
    rep(j, c) {
      if (j + 1 > K)
        break;
      tmp += A[j];
      ans = max(tmp, ans);
      int e = (K - (j + 1)) / c;
      if (e > 0) {
        ans = max(ans, tmp + psum * e);
      }
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a) for (int i = 0; i < (a); i++)

typedef pair<int, int> P;
typedef long long ll;

const int INF = 1001001001;
const int MOD = 1000000007;
const double PI = acos(-1);

int main() {
  int N;
  ll K;
  cin >> N >> K;
  ll P[N], C[N];
  rep(i, N) {
    ll p;
    cin >> p;
    p--;
    P[i] = p;
  }
  rep(i, N) {
    ll c;
    cin >> c;
    C[i] = c;
  }
  ll ans = -1e18;
  rep(i, N) {
    ll tmp = 0;
    set<int> seen;
    vector<ll> A;
    ll psum = 0;
    ll k = K;
    int s = i;
    int c = 0;
    // seen.insert(s);
    bool res = false;
    while (!seen.count(P[s])) {
      // cout << P[s] << endl;
      if (A.size() == 0) {
        A.push_back(C[P[s]]);
      } else {
        A.push_back(A.back() + C[P[s]]);
      }
      psum += C[P[s]];
      s = P[s];
      k--;
      c++;
      ans = max(ans, psum);
      if (k == 0) {
        res = true;
        break;
      }
    }
    if (res) {
      tmp = psum;
      ans = max(ans, psum);

    } else {
      tmp = psum;
      tmp += psum * (k / c);
      k %= c;
      tmp += A[k];
      ans = max(tmp, ans);
    }
  }
  cout << ans << endl;
}