fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using ll = long long;
  5.  
  6. const int N = 2e5 + 5;
  7. int p[N], a[N];
  8.  
  9. ll calc(int x, int n, int k) {
  10. ll cur = 0;
  11. ll mx = 0;
  12. int i = 0;
  13. vector<bool> vis(n);
  14. while (!vis[x] && i < min(n, k)) {
  15. vis[x] = true;
  16. mx = max(mx, cur + 1ll * a[x] * (k - i));
  17. cur += a[x];
  18. x = p[x];
  19. i++;
  20. }
  21. return mx;
  22. }
  23.  
  24. void solve() {
  25. int n, k, B, S;
  26. cin >> n >> k >> B >> S;
  27. B--; S--;
  28. for (int i = 0; i < n; i++) {
  29. cin >> p[i];
  30. p[i]--;
  31. }
  32. for (int i = 0; i < n; i++) cin >> a[i];
  33.  
  34. ll b_score = calc(B, n, k);
  35. ll s_score = calc(S, n, k);
  36.  
  37. if (b_score > s_score) {
  38. cout << "Bodya" << endl;
  39. } else if (b_score < s_score) {
  40. cout << "Sasha" << endl;
  41. }
  42. else cout << "Draw" << endl;
  43. }
  44.  
  45. int main() {
  46. int t;
  47. cin >> t;
  48. while (t--) solve();
  49. }
Success #stdin #stdout 0.01s 5300KB
stdin
10
4 2 3 2
4 1 2 3
7 2 5 6
10 8 2 10
3 1 4 5 2 7 8 10 6 9
5 10 5 1 3 7 10 15 4 3
2 1000000000 1 2
1 2
4 4
8 10 4 1
5 1 4 3 2 8 6 7
1 1 2 1 2 100 101 102
5 1 2 5
1 2 4 5 3
4 6 9 4 2
4 2 3 1
4 1 3 2
6 8 5 3
6 9 5 4
6 1 3 5 2 4
6 9 8 9 5 10
4 8 4 2
2 3 4 1
5 2 8 7
4 2 3 1
4 1 3 2
6 8 5 3
2 1000000000 1 2
1 2
1000000000 2
stdout
Bodya
Sasha
Draw
Draw
Bodya
Sasha
Sasha
Sasha
Sasha
Bodya