1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
#include <iostream>

using namespace std;

long long transform[500000][2];
long long tems,ucz;

struct Node {
    long long data;
    struct Node* prev, *next;
};

struct Node* getNode(long long data)
{
    struct Node* newNode =
            (struct Node*)malloc(sizeof(struct Node));

    newNode->data = data;
    newNode->prev = newNode->next = NULL;
    return newNode;
}

bool is_higher(long long me, long long next){

    long long diff = 0;

    for (long i = next+1; i <= me; i++) {
        diff = diff += transform[i][0] * transform[i][1];
    }
    if (diff >= 0)
        return true;
    return false;
}

void sortedInsert(struct Node** head_ref, struct Node* newNode)
{
    struct Node* current;


    if (*head_ref == NULL) {
        *head_ref = newNode;
        return;
    }


    if (!is_higher(newNode->data,(*head_ref)->data)) {
        newNode->next = *head_ref;
        newNode->next->prev = newNode;
        *head_ref = newNode;
    }

    else {
        current = *head_ref;

        while (current->next != NULL &&
                is_higher(newNode->data, current->next->data))
            current = current->next;

        newNode->next = current->next;

        if (current->next != NULL)
            newNode->next->prev = newNode;

        current->next = newNode;
        newNode->prev = current;
    }
}

void print(struct Node** head_ref){
    struct Node* current = *head_ref;

    while (current->next != NULL)
        cout << current->data;
}




int main() {

    long long idx,val;

    cin >> tems >> ucz;
    long al[ucz];

    for (long i=0; i<tems; i++) {
        cin >> val;
        al[i] = val;
    }

    struct Node* head = NULL;
    struct Node* new_node = getNode(0);
    sortedInsert(&head, new_node);

    for (long i = 1; i <= ucz; i++) {
        cin >> idx >> val;
        idx;
        transform[i][0] = idx;
        transform[i][1] = val - al[idx];
        al[idx] = val;
        new_node = getNode(i);
        sortedInsert(&head, new_node);
    }

    print(&head);
    
    return 0;
}