#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct Node {
int coef;
int exp;
struct Node *next;
}Node, *LinkedList;
LinkedList insert(LinkedList head, Node *node, int index) {
if (head == NULL) {
if (index != 0) {
return head;
}
head = node;
return head;
}
if (index == 0) {
node->next = head->next;
head->next = node;
return head;
}
Node *current_node = head;
int count = 0;
while (current_node->next != NULL && count < index - 1) {
current_node = current_node->next;
count++;
}
if (count == index - 1) {
node->next = current_node->next;
current_node->next = node;
}
return head;
}
void output(LinkedList head) {
if (head == NULL) {
return;
}
Node *current_node = head;
while (current_node != NULL) {
printf("coef = %d, exp = %d\n", current_node->coef, current_node->exp);
current_node = current_node->next;
}
}
void outputSparsepolynomial(LinkedList head, int num) {
int sum = 0;
if (head == NULL) {
return;
}
Node *current_node = head;
while (current_node != NULL) {
sum += current_node->coef * pow((double)num, (double)current_node->exp);
current_node = current_node->next;
}
printf("%d\n", sum);
}
void clear(LinkedList head) {
if (head == NULL) {
return;
}
Node *current_node = head;
while (current_node != NULL) {
Node *delete_node = current_node;
current_node = current_node->next;
free(delete_node);
}
}
int main() {
LinkedList linkedlist = NULL;
int m, co, ex, num; //m 总行数,co 各项系数c,ex 各项次数,num 为 x 值
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%d %d", &co, &ex);
Node *node = (Node *)malloc(sizeof(Node));
node->coef = co;
node->exp = ex;
node->next = NULL;
linkedlist = insert(linkedlist, node, i - 1);
}
scanf("%d", &num);
// output(linkedlist);
outputSparsepolynomial(linkedlist, num);
clear(linkedlist);
return 0;
}