# Vending Machines (vendingmachines)

**URL:** <https://forum.olinfo.it/t/vending-machines-vendingmachines/9039>\
**Category:** Aiuto\
**Created:** [6 Ottobre 2024, 8:29pm UTC](https://forum.olinfo.it/t/vending-machines-vendingmachines/9039 "2024-10-06T20:29:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![russoangelo](https://forum.olinfo.it/user_avatar/forum.olinfo.it/russoangelo/32/1996_2.png) [@russoangelo](https://forum.olinfo.it/u/russoangelo)\
**Post date:** [6 Ottobre 2024, 8:29pm UTC](https://forum.olinfo.it/t/vending-machines-vendingmachines/9039/1 "2024-10-06T20:29:16Z")

</div>

Mi dice

```auto
terminate called after throwing an instance of 'std::length_error'
  what(): cannot create std::vector larger than max_size()

```

con questo input  
2  
5 5  
4 14 14 30 24  
-4 +19 -5 +27 +27  
10 10  
22 14 29 23 22 23 26 6 22 2  
+11 +27 +18 -6 +9 -8 -3 -10 +30 -4

ma se lo divido l’input é corretto ovvero  
1  
5 5  
4 14 14 30 24  
-4 +19 -5 +27 +27

1  
10 10  
22 14 29 23 22 23 26 6 22 2  
+11 +27 +18 -6 +9 -8 -3 -10 +30 -4

```auto
#include <bits/stdc++.h>
using namespace std;

void run_case(int t){
    for(int i=0; i<t; i++){

        int n, q;
        int sum = 0;
        bool isHacker = false;

        cin >> n >> q;

        vector<int> P(n), L(q);

        for(int j=0; j<n; j++){
            cin >> P[j];
        }
        for(int j=0; j<q; j++){
            cin >> L[j];
            if(L[j]>=0){
                sum += L[j];
            } else {
                int index = abs(L[j]) - 1;
                if (index < n) {
                    sum -= P[index];
                }
            }

            if(sum < 0){
                isHacker = true;
                break;
            }
        }

        cout << (isHacker ? "HACKER" : "OK") << endl;
    }
}

int main() {
    int t;
    cin >> t;

    run_case(t);
}

```

---

<div class="post-metadata">

**Author:** ![D\_Dario](https://forum.olinfo.it/user_avatar/forum.olinfo.it/d_dario/32/2015_2.png) [@D\_Dario](https://forum.olinfo.it/u/D_Dario)\
**Post date:** [20 Novembre 2024, 1:12am UTC](https://forum.olinfo.it/t/vending-machines-vendingmachines/9039/2 "2024-11-20T01:12:13Z")

</div>

Ciao,  
se leggi il tuo codice attentamente puoi notare che l’errore è nella lettura dell’input, perché il ruolo di ogni numero dipende dalla sua posizione. In particolare il tuo codice “dimentica” di leggere tutti i valori di L[i] quando riconosce un hacker perché esce dal ciclo in cui legge i valori delle transazioni.  
Spero di essere stato chiaro.
