Language: C++

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>

#define max(a,b) ((a > b) ? a : b)
#define min(a,b) ((a < b) ? a : b)
#define abs(a) (((a) < 0) ? -(a) : (a))

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;

typedef struct {
	int num;
	int r;
	int l;
	bool used;
} Ku;

static int compl(const void *a, const void *b) {
	return (*(Ku**)a)->l - (*(Ku**)b)->l;
}
static int compr(const void *a, const void *b) {
	return (*(Ku**)a)->r - (*(Ku**)b)->r;
}


static inline int ri() {
	int a;
	scanf("%d", &a);
	return a;
}

int main() {
	int n = ri();
	
	Ku k[n];
	Ku *l[n];
	Ku *r[n];
	for (int i = 0; i < n; i++) {
		k[i].num = i;
		k[i].l = ri();
		k[i].r = ri();
		k[i].used = false;
		l[i] = &k[i];
		r[i] = &k[i];
	}
	
	qsort(l, n, sizeof(Ku*), compl);
	qsort(r, n, sizeof(Ku*), compr);
	
	/** DEBUGGGGGG */
	/*
	for (int i = 0; i < n; i++) {
		printf("l : %d, r : %d\n", l[i]->l, r[i]->r);
	}
	*/
	
	u64 res = 0;
	int cur_pos = 0;
	int used = 0;
	while(1) {
		// select the rightest
		int right = -1;
		for (int i = n - 1; i >= 0; i--) {
			if (!l[i]->used) {
				l[i]->used = true;
				right = i;
				used++;
				/** DEBUGGGGGG */
				// printf("rightest/l : %d\n", l[i]->num);
				break;
			}
		}
		if (right == -1) while(1); // unexpected
		if (l[right]->l <= cur_pos && l[right]->r >= cur_pos) {} // ------[---.---]----------
		else if (l[right]->l <= cur_pos) { // ------[-----]---.-------
			res += (u64)abs(cur_pos - l[right]->r);
			cur_pos = l[right]->r;
			/** DEBUGGGGGGG */
			// printf("-----[------]---.-----\ncur_pos = %d\n", cur_pos);
		} else { // ---.---[-----]----------
			res += (u64)abs(cur_pos - l[right]->l);
			cur_pos = l[right]->l;
			/** DEBUGGGGGGG */
			// printf("---.---[-----]----------\ncur_pos = %d\n", cur_pos);
		}
		
		if (used >= n) break;
		
		
		// select the leftest
		int left = -1;
		for (int i = 0; i < n; i++) {
			if (!r[i]->used) {
				r[i]->used = true;
				left = i;
				used++;
				/** DEBUGGGGGG */
				// printf("leftest/r : %d\n", r[i]->num);
				break;
			}
		}
		if (left == -1) while(1); // unexpected
		if (r[left]->l <= cur_pos && r[left]->r >= cur_pos) {} // ------[---.---]----------
		else if (r[left]->l <= cur_pos) { // ------[-----]---.-------
			res += (u64)abs(cur_pos - r[left]->r);
			cur_pos = r[left]->r;
			/** DEBUGGGGGGG */
			// printf("------[-----]---.-------\ncur_pos = %d\n", cur_pos);
		} else { // ---.---[-----]----------
			res += (u64)abs(cur_pos - r[left]->l);
			cur_pos = r[left]->l;
			/** DEBUGGGGGGG */
			// printf("---.---[-----]----------\ncur_pos = %d\n", cur_pos);
		}
		
		if (used >= n) break;
	}
	
	res += (u64) abs(cur_pos);
	
	// printf("%lld", res);
	printf("%I64u", res);
	
	return 0;
}


Compiler output:
a.cc:26:17: error: expected class-name before '(' token
   26 | static int compl(const void *a, const void *b) {
      |                 ^
a.cc: In function 'int main()':
a.cc:55:39: error: expected primary-expression before ')' token
   55 |         qsort(l, n, sizeof(Ku*), compl);
      |                                       ^