CTNEWS - Lâu đài cát

Tác giả: ll931110

Ngôn ngữ: C++

#include <iostream>
#include <cstdlib>
#define MAXN 25000
using namespace std;

int compare(const void* u, const void* v)
{
    int* x1 = (int*) u;
    int* y1 = (int*) v;
    
    if (*x1 > *y1) return -1;
    else if (*x1 == *y1) return 0;
    else return 1;
}

int a[MAXN],b[MAXN],n,x,y;
long long cost;

int main()
{
    int i;
    
    //freopen("ctnews.inp","r",stdin);
    //freopen("c1.out","w",stdout);
    
    scanf("%d%d%d", &n, &x, &y);
    for (i = 0; i < n; i++) scanf("%d%d", &a[i], &b[i]);
    
    qsort(a, n, sizeof(int), compare);
    qsort(b, n, sizeof(int), compare);
    
    cost = 0;
    for (i = 0; i < n; i++)
      if (a[i] > b[i]) cost += y * (a[i] - b[i]);
      else if (a[i] < b[i]) cost += x * (b[i] - a[i]);
      
    printf("%lld", cost);
}

Download