#include #include struct tah { int a,b; bool sa,sb; bool used; }; tah tahy[400]; int ssa,ssb; int compa(const void* x, const void* y) { if (((tah*)x)->a < ssa) return 1; if (((tah*)y)->a < ssa) return -1; if (((tah*)x)->sa && !((tah*)y)->sa) return 1; if (((tah*)x)->a > ((tah*)y)->a) return 1; else return -1; } int compb(const void* x, const void* y) { if (((tah*)x)->b < ssb) return 1; if (((tah*)y)->b < ssb) return -1; if (((tah*)x)->sb && !((tah*)y)->sb) return 1; if (((tah*)x)->b > ((tah*)y)->b) return 1; else return -1; } int main(void) { for(;;){ bool taha = true; int a,b,ntahy; scanf("%d %d %d\n", &a,&b,&ntahy); for (int i = 0; i < ntahy; i++) { int pa,pb; char ca,cb; scanf("%d %d %c %c\n",&pa,&pb,&ca,&cb); tahy[i].a = pa; tahy[i].b = pb; if (ca == 'S') {tahy[i].sa = true; ssa += pa;} if (cb == 'S') {tahy[i].sa = true; ssb += pb;} tahy[i].used=false; } for (int i = 0; i < ntahy; i++) { if (taha) { qsort(tahy, ntahy, sizeof(tah), compa); int j =i; while (tahy[j].used){j++;}; a += tahy[j].a; tahy[j].used=true; if (tahy[j].sa) ssa -= tahy[j].a; } else { qsort(tahy, ntahy, sizeof(tah), compb); int j = i; while (tahy[j].used) {j++;}; b += tahy[j].b; tahy[j].used=true; if (tahy[j].sb) ssb -= tahy[j].b; } // tahy[j].used=true; taha = !taha; /* for (int k = 0; k < ntahy; k++) { printf("ddd %d %d %d\n",tahy[k].a,tahy[k].b,tahy[k].used); } */ } printf("%d %d\n", a, b); if (feof(stdin)) break; } return 0; }