summaryrefslogblamecommitdiffstats
path: root/inf/lige/1/2.c
blob: b96109388859508b44a35d2c0b48393ff314f88f (plain) (tree)





































































                                                                                                   
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (void) {
	char buf[128];
	fgets(buf, 128, stdin);
	char * c = buf;
	int n = strtol(c, &c, 10);
	c++;
	int k = strtol(c, &c, 10);
	int * zx = calloc(1000000, sizeof *zx);
	int * zy = calloc(1000000, sizeof *zy);
	int x = 0;
	int y = 0;
	int num = 0;
	int vseh = 0;
	while (1) {
		char a = getchar();
		switch (a) {
			case EOF:
				goto x;
			case '\r':
				break;
			case '\n':
#ifndef EVAL
				fprintf(stderr, "newline\n");
#endif
				if (num) {
					zx[num-1] = x;
					zy[num-1] = y;
					num = 0;
					vseh++;
				}
				x = 0;
				y++;
				break;
			case ' ':
#ifndef EVAL
				fprintf(stderr, "presledek\n");
#endif
				if (num) {
					zx[num-1] = x;
					zy[num-1] = y;
					num = 0;
					vseh++;
				}
				x++;
				break;
			default:
#ifndef EVAL
				fprintf(stderr, "številka %c\n", a);
#endif
				num *= 10;
				num += a - '0';
		}
	}
x:;
	long long int sum = 0;
	int curx = 0;
	int cury = 0;
	for (int i = 0; i < vseh; i++) {
		sum += abs(curx - zx[i]) + abs(cury - zy[i]);
#ifndef EVAL
		fprintf(stderr, "znamenitost %d je na %d %d, sum je %lld\n", i, zx[i], zy[i], sum);
#endif
		curx = zx[i];
		cury = zy[i];
	}
	printf("%lld\n", sum);
}