#include #include #include #define MAX 50000 char line[MAX]; char line2[MAX]; char *l; int sum; int zpracuj(char co, int h, int m) { char *last=l; int cnt=0; for (char *ptr=l;*ptr;ptr++) { if ( (*ptr==co) && cnt=m) { if (co!='x' && co!='c') break; char str[3]="ix"; if (co=='c') {str[0]='x';str[1]='c';} char *first=strchr(last,str[0]); if (!first) break; char *second=strchr(first+1,str[1]); if (!second) break; l=second+1; sum+=(str[0]=='i') ? 9 : 90; return 1; } } if (last) l=last; return 0; } int main(void) { while (fgets(line2,MAX,stdin)) { char *p=line2; char *n=line; while (*p) { if (*p=='m' || *p=='d' || *p=='l' || *p=='c' || *p=='x' || *p=='v' || *p=='i') *n++=*p; p++; } *n='\0'; l=line; sum=0; zpracuj('m',1000,MAX); zpracuj('d',500,1); if (!zpracuj('c',100,3)) { zpracuj('l',50,1); if (!zpracuj('x',10,3)) { zpracuj('v',5,1); zpracuj('i',1,3); } } else { if (!zpracuj('x',10,0)) { zpracuj('v',5,1); zpracuj('i',1,3); } } printf("%d\n",sum); } return 0; }