#include #include #define MAX_LENGTH 165 int main(void) { char buf[MAX_LENGTH]; int color_num, sum, max_r, max_g, max_b; sum = 0; while (fgets(buf, MAX_LENGTH, stdin)) { char* c = buf + 5; // +5 skips the "Game " for (; *c != ':'; c++); max_r = max_g = max_b = 0; while (*c != '\n') { color_num = strtol(c + 2, &c, 10); // +2 skips ": ", ", " or "; " c++; // +1 skips the space max_r = (*c == 'r' && color_num > max_r) ? color_num : max_r; max_g = (*c == 'g' && color_num > max_g) ? color_num : max_g; max_b = (*c == 'b' && color_num > max_b) ? color_num : max_b; for (; *c >= 'a' && *c <= 'z'; c++); } sum += max_r * max_g * max_b; } printf("%d\n", sum); return 0; }