This is what I had in mind:
Code:
#include <stdio.h>
#include <stdlib.h>
#define min(a, b) ((a) < (b) ? (a) : (b))
struct values
{
unsigned long long int C;
long long int T;
};
int compare(const void *x, const void *y)
{
const struct values *ptr = x;
const struct values *ptr1 = y;
if (ptr->C < ptr1->C)
return -1;
else if (ptr->C > ptr1->C)
return 1;
else
return 0;
}
int main(void)
{
long long int N, M;
int H, j;
scanf("%lld %lld %d", &N, &M, &H);
long long int toPaint = N * M;
long long int ans = 0;
long long int canPaint;
struct values values_list[H];
int i;
for (i = 0; i < H; i++)
{
scanf("%lld %llu", &values_list[i].T, &values_list[i].C);
}
qsort(values_list, H, sizeof(values_list[0]), compare);
for (i = 0; i < H; i++)
{
canPaint = min(toPaint, values_list[i].T);
ans += canPaint * values_list[i].C;
toPaint -= canPaint;
}
if (toPaint > 0)
printf("Impossible\n");
else
printf("%lld\n", ans);
return 0;
}
Notice that I kept to your code in post #1, except that I changed from using two variable length arrays
T and
C to a single variable length array
values_list. Wherever you had T[i], I changed it to values_list[i].T, and likewise for C. I added the #include <stdlib.h> because you're now using qsort. Also, notice that you originally had T as an array of long long int, so hence the T member in this version is a long long int rather than an unsigned long long int as in your post #3. Furthermore, to read an unsigned long long int, I used %llu instead of %lld.
Oh, and observe the "extra" parentheses that I added for the macro. This is good practice to avoid potential problems with precedence.