#include using std::cin; using std::cout; using std::endl; using ll = long long; using std::vector; #define rep(i,j,k) for(int i=int(j); i> N >> M >> K; vector> data(N); rep(i,0,M){ ll index, val; cin >> index >> val; data[index-1].push_back(val); } ll sum = 0; vector val(N), now(N); bool flag = false; rep(i,0,N){ sort(data[i].begin(), data[i].end()); if(data[i].empty()){ flag = true; now[i] = -1; continue; } now[i] = data[i].size()/2; val[i] = data[i][data[i].size()/2]; sum += val[i]; } if(sum <= K){ if(false){ bool mod2 = 1; while(sum < K){ rep(i,0,N){ if(sum == K) break; if(mod2 != data[i].size()%2) continue; if(now[i] == data[i].size()-1){ val[i] += K-sum; sum = K; break; } if(K-sum <= data[i][now[i]+1]-data[i][now[i]]){ val[i] += K-sum; sum = K; break; } now[i]++; val[i] = data[i][now[i]]; sum += data[i][now[i]]-data[i][now[i]-1]; } mod2 ^= 1; } } } else{ bool mod2 = 0; while(K < sum){ rep(i,0,N){ if(sum == K) break; if(now[i] == -1) continue; if(mod2 != data[i].size()%2) continue; ll wid = data[i][now[i]]; if(0 < now[i]) wid -= data[i][now[i]-1]; if(sum-K <= wid){ val[i] -= sum-K; sum = K; break; } now[i]--; val[i] -= wid; sum -= wid; } mod2 ^= 1; } } ll ans = 0; rep(i,0,N){ for(auto p:data[i]){ ans += std::abs(val[i]-p); } } cout << ans << endl; }