#include <bits/stdc++.h>
using
namespace
std;
class
Node {
public
:
int
data;
Node* next;
};
Node* detectLoop(Node* head)
{
Node *slow = head, *fast = head;
while
(slow && fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if
(slow == fast) {
return
slow;
}
}
return
NULL;
}
void
push(Node** head_ref,
int
new_data)
{
Node* new_node =
new
Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int
findMinCost(vector<
int
> arr)
{
int
avg = arr[0];
for
(
int
i = 1; i < arr.size(); i++) {
avg = (avg + arr[i]) / 2;
}
int
ans = 0;
for
(
int
i = 0; i < arr.size(); i++) {
ans +=
abs
(avg - arr[i]);
}
return
ans;
}
int
minimumCost(Node* head)
{
Node* temp = detectLoop(head);
if
(!temp)
return
0;
Node* temp2 = temp->next;
vector<
int
> arr;
if
(temp->data % 2 == 0)
arr.push_back(temp->data);
while
(temp2 != temp) {
if
(temp2->data % 2 == 0)
arr.push_back(temp2->data);
temp2 = temp2->next;
}
if
(arr.size() > 1) {
return
findMinCost(arr);
}
else
{
return
0;
}
}
int
main()
{
Node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 10);
head->next->next->next->next = head;
cout << minimumCost(head);
return
0;
}