#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <iostream>
struct Pair {
double coeff, cost;
int id;
};
bool operator ==(const Pair& a, const Pair& b) {
return a.coeff == b.coeff && a.cost == b.cost;
}
// Генерация теста на n элементов:
std::vector<Pair> gen(const int n) {
std::vector<Pair> arr;
arr.resize(n);
for (int i = 0; i < n; ++i) {
arr[i].coeff = 1LL * std::rand() * std::rand() % 10000000 * 1e-6;
arr[i].cost = std::rand() % 100 + 1;
arr[i].id = i+1;
}
return arr;
}
// Неправильное решение:
std::vector<Pair> solve1(std::vector<Pair> arr) {
std::stable_sort(arr.begin(), arr.end(), [](const Pair& a, const Pair& b){
return (a.coeff-1) / a.cost > (b.coeff-1) / b.cost;
});
return arr;
}
// Правильное решение:
std::vector<Pair> solve2(std::vector<Pair> arr) {
std::stable_sort(arr.begin(), arr.end(), [](const Pair& a, const Pair& b) {
const int Ai = (a.coeff < 1) - (a.coeff > 1);
const int Aj = (b.coeff < 1) - (b.coeff > 1);
if (Ai != Aj) {
return Ai < Aj;
}
if (Ai == 0 && Aj == 0) {
return false;
}
return a.cost / (a.coeff - 1) < b.cost / (b.coeff - 1);
});
return arr;
}
std::vector<Pair> input() {
int n; scanf("%d %*d", &n);
std::vector<Pair> arr(n);
for (int i = 0; i < n; ++i) {
scanf("%lf %lf", &arr[i].coeff, &arr[i].cost);
arr[i].id = i+1;
}
return arr;
}
bool equal(const std::vector<Pair>& arr1, const std::vector<Pair>& arr2) {
if (arr1 == arr2) {
return true;
}
long double sum1 = 0;
for (auto& it : arr1) {
(sum1 *= it.coeff) -= it.cost;
}
long double sum2 = 0;
for (auto& it : arr2) {
(sum2 *= it.coeff) -= it.cost;
}
return std::abs(sum1 - sum2) < 1e-18L;
}
int main() {
std::srand(std::time(0));
//gen();
auto arr = input();
auto arr1 = solve1(arr);
auto arr2 = solve2(arr);
assert(equal(arr1, arr2));
for (auto& it : arr1) {
printf("%d\n",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