Problem:
A：沢山の種類の林檎 (Many Kinds of Apples)
Problem Statement
Apple Farmer Mon has two kinds of tasks: "harvest apples" and "ship apples".
There are
N
different species of apples, and
N
distinguishable boxes. Apples are labeled by the species, and boxes are also labeled, from
1
to
N
. The
i
-th species of apples are stored in the
i
-th box.
For each
i
, the
i
-th box can store at most
c_i
apples, and it is initially empty (no apple exists).
Mon receives Q instructions from his boss Kukui, and Mon completely follows in order. Each instruction is either of two types below.
"harvest apples": put
d
x
-th apples into the
x
-th box.
"ship apples": take
d
x
-th apples out from the
x
-th box.
However, not all instructions are possible to carry out. Now we call an instruction which meets either of following conditions "impossible instruction":
When Mon harvest apples, the amount of apples exceeds the capacity of that box.
When Mon tries to ship apples, there are not enough apples to ship.
Your task is to detect the instruction which is impossible to carry out.
Input
Input is given in the following format.
N
c_1
c_2
$\cdots$
c_N
Q
t_1
x_1
d_1
t_2
x_2
d_2
$\vdots$
t_Q
x_Q
d_Q
In line 1, you are given the integer
N
, which indicates the number of species of apples.
In line 2, given
c_i
(
1 \leq i \leq N
) separated by whitespaces.
c_i
indicates the capacity of the
i
-th box.
In line 3, given
Q
, which indicates the number of instructions.
Instructions are given successive
Q
lines.
t_i
x_i
d_i
means what kind of instruction, which apple Mon handles in this instruction, how many apples Mon handles, respectively. If
t_i
is equal to
1
, it means Mon does the task of "harvest apples", else if
t_i
is equal to
2
, it means Mon does the task of "ship apples".
Constraints
All input values are integers, and satisfy following constraints.
1 \leq N \leq 1,000
1 \leq c_i \leq 100,000
(
1 \leq i \leq N
)
1 \leq Q \leq 100,000
t_i \in \{1, 2\}
(
1 \leq i \leq Q
)
1 \leq x_i \leq N
(
1 \leq i \leq Q
)
1 \leq d_i \leq 100,000
(
1 \leq i \leq Q
)
Output
If there is "impossible instruction", output the index of the apples which have something to do with the first "impossible instruction".
Otherwise, output
0
.
Sample Input 1
2
3 3
4
1 1 2
1 2 3
2 1 3
2 2 3
Sample Output 1
1
In this case, there are not enough apples to ship in the first box.
Sample Input 2
2
3 3
4
1 1 3
2 1 2
1 2 3
1 1 3
Sample Output 2
1
In this case, the amount of apples exceeds the capacity of the first box.
Sample Input 3
3
3 4 5
4
1 1 3
1 2 3
1 3 5
2 2 2
Sample Output 3
0
Sample Input 4
6
28 56 99 3 125 37
10
1 1 10
1 1 14
1 3 90
1 5 10
2 3 38
2 1 5
1 3 92
1 6 18
2 5 9
2 1 4
Sample Output 4
3

Submitted solutions:

code_snippet: #include <bits/stdc++.h>


using namespace std;
//make -f ../makefile SRC=
/*
*/


//------------------------------------------------------------------------------
bool DEBUG = false;
const int INF = 1000000000;

const int MAX_N = 1000+5;
static int capacity[MAX_N];
static int apples[MAX_N];

const int ADD = 1;
const int DEL = 2;
//------------------------------------------------------------------------------
bool update(int N, int type, int kth, int v)
{
    if (kth-1 >= N) return false;

    if (type == ADD)
    {
        if (v + apples[kth-1] > capacity[kth-1])
        {
            apples[kth-1] = capacity[kth-1];
            return false;
        }
        else apples[kth-1] += v;
    }
    else if (type == DEL)
    {
        if (v > apples[kth-1])
        {
            apples[kth-1] = 0;
            return false;
        }
        else apples[kth-1] -= v;
    }

    return true;
}

//------------------------------------------------------------------------------
int main()
{
    //test(); return 0;
    //DEBUG = true;
    //--------------------------------------------------------------------------
    int N, Q, type, kth, v, num;
    num = scanf("%d ", &N);
    for (int i=0; i<N; ++i) num = scanf("%d ", &capacity[i]);

    for (int i=0; i<N; ++i) apples[i] = 0;
    bool ok = true;
    int res = -1; // pos at first error
    num = scanf("%d ", &Q);
    for (int q=0; q<Q; ++q)
    {
        num = scanf("%d %d %d ", &type, &kth, &v);
        if (!update(N, type, kth, v))
        {
            if (res == -1) { ok = false; res = kth; }
        }
    }
    if (ok) printf("0\n");
    else printf("%d\n", res);
    //--------------------------------------------------------------------------
    return 0;
}
//------------------------------------------------------------------------------
accuracy: 1
time_ms: 10
memory_kb: 3564
final_rank: 9

code_snippet: #line 2 "/home/sakflat/CP/_library/cpp/template/template.cpp"

//yukicoder@cpp17

#include <iostream>
#include <iomanip>

#include <algorithm>

#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>

#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>

#include <random>

#include <bitset>

#include <complex>

#include <utility>

#include <numeric>

#include <functional>


using namespace std;
using ll = long long;
using P = pair<ll,ll>;


const ll MOD = 998244353;
const ll MODx = 1000000007;
const int INF = (1<<30)-1;
const ll LINF = (1LL<<62LL)-1;
const double EPS = (1e-10);


P ar4[4] = {{0,1},{0,-1},{1,0},{-1,0}};
P ar8[8] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};


template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
 
/*
確認ポイント
cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる

計算量は変わらないが楽できるシリーズ
min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる
count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる
*/

/* comment outed because can cause bugs
__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
*/
#line 2 "/home/sakflat/CP/_library/cpp/template/basic.cpp"

int main(){
  int n;cin>>n;
  vector<int> A(n),B(n,0);
  for(int i = 0; n > i; i++){
    cin>>A[i];
  }
  int q;cin>>q;
  for(int i = 0; q > i; i++){
    int t,x,d;cin>>t>>x>>d;x--;
    if(t == 1){
      B[x]+=d;
      if(B[x] > A[x]){
        cout << x+1 << endl;
        return 0;
      }
    }else if(t == 2){
      B[x]-=d;
      if(B[x] < 0){
        cout << x+1 << endl;
        return 0;
      }
    }
  }
  cout << 0 << endl;
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3436
final_rank: 13

code_snippet: #include <bits/stdc++.h>

int main() {
    int N;
    std::cin >> N;
    std::vector<int> C(N);
    for (auto& c : C) std::cin >> c;
    int Q;
    std::cin >> Q;
    std::vector<int> cur(N);
    while (Q--) {
        int t, x, d;
        std::cin >> t >> x >> d;
        x--;
        if (t == 1) {
            cur[x] += d;
            if (cur[x] > C[x]) {
                std::cout << x + 1 << '\n';
                return 0;
            }
        }
        else if (t == 2) {
            cur[x] -= d;
            if (cur[x] < 0) {
                std::cout << x + 1 << '\n';
                return 0;
            }
        }
    }
    std::cout << 0 << '\n';
}
accuracy: 1
time_ms: 40
memory_kb: 3436
final_rank: 15

code_snippet: #include <bits/stdc++.h>

int main() {
    int n;
    std::cin >> n;
    std::vector<int> c(n);
    for (int i = 0; i < n; i++) std::cin >> c[i];
    std::vector<int> a(n);
    int q;
    std::cin >> q;
    for (int i = 1; i <= q; i++) {
        int t, x, d;
        std::cin >> t >> x >> d;
        x--;
        if (t == 1) {
            if (a[x] + d > c[x]) {
                std::cout << x + 1 << '\n';
                return 0;
            }
            a[x] += d;
        } else {
            if (a[x] < d) {
                std::cout << x + 1 << '\n';
                return 0;
            }
            a[x] -= d;
        }
    }
    std::cout << 0 << std::endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3380
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;

int main(){
    int N; cin >> N;
    vector<int> C(N);
    for(int i = 0; i < N; i++) cin >> C[i];

    vector<int> box(N);
    int Q; cin >> Q;
    for(int i = 0; i < Q; i++){
        int t, x, d; cin >> t >> x >> d;
        x--;
        if(t == 1){
            if(box[x] + d > C[x]){
                cout << x + 1 << endl;
                return 0;
            }
            else box[x] += d;
        }
        else{
            if(box[x] - d < 0){
                cout << x + 1 << endl;
                return 0;
            }
            else box[x] -= d;
        }
    }

    cout << 0 << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3436
final_rank: 13

code_snippet: #include <iostream>
#include <vector>
using namespace std;
int main(){
    int n;cin>>n;
    vector<int> A(n);
    for(int i = 0; n > i; i++)cin>>A[i];
    vector<int> C(n,0);
    int q;cin>>q;
    for(int i = 0; q > i; i++){
        int t,x,d;cin>>t>>x>>d;
        x--;
        if(t == 1){
            C[x]+=d;
            if(C[x] > A[x]){
                cout << x+1 << endl;
                return 0;
            }
        }else{
            C[x]-=d;
            if(C[x] < 0){
                cout << x+1 << endl;
                return 0;
            }
        }
    }
    cout << 0 << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 3128
final_rank: 20

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int> a(n), c(n);
    for (int i = 0; i < n; i++) {
        cin >> c[i];
    }
    int q;
    cin >> q;
    while (q--) {
        int t, x, d;
        cin >> t >> x >> d;
        x -= 1;
        if (t == 1) {
            a[x] += d;
            if (a[x] > c[x]) {
                cout << x + 1 << endl;
                return 0;
            }
        } else {
            a[x] -= d;
            if (a[x] < 0) {
                cout << x + 1 << endl;
                return 0;
            }
        }
    }
    cout << 0 << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3464
final_rank: 7

code_snippet: #include <bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
#define all(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}
template<class T>
bool chmin(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}
ll modPow(long long a, long long n, long long p) {
    if (n == 0) return 1; // 0乗にも対応する場合
    if (n == 1) return a % p;
    if (n % 2 == 1) return (a * modPow(a, n - 1, p)) % p;
    long long t = modPow(a, n / 2, p);
    return (t * t) % p;
}
ll cnt = 0;
ll gcd(ll(a), ll(b)) {
    cnt++;
    if (a == 0)return b;
    if (b == 0)return a;
    ll c = a;
    while (a % b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return b;
}
ll sqrtz(ll N) {
    ll L = 0;
    ll R = sqrt(N) + 10000;
    while (abs(R - L) > 1) {
        ll mid = (R + L) / 2;
        if (mid * mid <= N)L = mid;
        else R = mid;
    }
    return L;
}

using LD = long double;


/*
using mint = modint998244353;
using vm = vector<mint>;
using vvm = vector<vm>;
using vvvm = vector<vvm>;


vector<mint> fact, factinv, inv;
const ll mod = 998244353;
void prenCkModp(ll n) {
    fact.resize(n + 5);
    factinv.resize(n + 5);
    inv.resize(n + 5);
    fact[0] = fact[1] = 1;
    factinv[0] = factinv[1] = 1;
    inv[1] = 1;
    for (ll i = 2; i < n + 5; i++) {
        fact[i] = (fact[i - 1] * i);
        inv[i] = (mod - ((inv[mod % i] * (mod / i))));
        factinv[i] = (factinv[i - 1] * inv[i]);
    }
}
mint nCk(ll n, ll k) {
    if (n < k || k < 0) return 0;
    return (fact[n] * ((factinv[k] * factinv[n - k])));
}
*/

int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N;
    cin>>N;
    vll C(N);
    rep(i,N)cin>>C[i];
    vll A(N,0);
    ll Q;
    cin>>Q;
    rep(q,Q){
        ll T,X,D;
        cin>>T>>X>>D;
        X--;
        if(T==1){
            if(A[X]+D>C[X]){
                cout<<X+1<<endl;
                return 0;
            }
            A[X]+=D;
        }
        else{
            if(A[X]<D){
                cout<<X+1<<endl;
                return 0;
            }
            A[X]-=D;
        }
    }
    cout<<0<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3496
final_rank: 8

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=2100000000;
const ll mod=998244353;
#define rep(i,a) for (ll i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}
template<class T> T min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}



void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,t) solve();
}

void solve(){
	int N;
	cin>>N;
	vector<int> C(N);
	vector<int> p(N);
	rep(i,N) cin>>C[i];
	int Q;
	cin>>Q;
	rep(i,Q){
		int t,x,d;
		cin>>t>>x>>d;
		if(t==1){
			p[x-1]+=d;
		}else{
			p[x-1]-=d;
		}
		if(p[x-1]<0||C[x-1]<p[x-1]){
			cout<<x<<"\n";
			return;
		}
	}
	cout<<"0\n";
}
accuracy: 1
time_ms: 10
memory_kb: 3448
final_rank: 6

code_snippet: #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

/* macro */

#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define rrep(i, a, n) for (int i = ((int)(n - 1)); i >= (int)(a); i--)
#define Rep(i, a, n) for (i64 i = (i64)(a); i < (i64)(n); i++)
#define RRep(i, a, n) for (i64 i = ((i64)(n - i64(1))); i >= (i64)(a); i--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define Bit(n) (1LL << (n))

/* macro end */

/* template */

namespace ebi {

#ifdef LOCAL
#define debug(...)                                                      \
    std::cerr << "LINE: " << __LINE__ << "  [" << #__VA_ARGS__ << "]:", \
        debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

void debug_out() { std::cerr << std::endl; }

template <typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
    std::cerr << " " << h;
    if (sizeof...(t) > 0) std::cout << " :";
    debug_out(t...);
}

template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &pa) {
    return os << pa.first << " " << pa.second;
}

template <typename T1, typename T2>
std::istream &operator>>(std::istream &os, std::pair<T1, T2> &pa) {
    return os >> pa.first >> pa.second;
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {
    for (std::size_t i = 0; i < vec.size(); i++)
        os << vec[i] << (i + 1 == vec.size() ? "" : " ");
    return os;
}

template <typename T>
std::istream &operator>>(std::istream &os, std::vector<T> &vec) {
    for (T &e : vec) std::cin >> e;
    return os;
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::optional<T> &opt) {
    if (opt) {
        os << opt.value();
    } else {
        os << "invalid value";
    }
    return os;
}

using std::size_t;
using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;

template <class T, T init>
auto make_vector(int n) {
    return std::vector<T>(n, init);
}

template <class T, T init, typename Head, typename... Tail>
auto make_vector(Head n, Tail... ts) {
    return std::vector(n, make_vector<T, init>(ts...));
}

template <class T>
inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
T pow(T x, i64 n) {
    T res = 1;
    while (n > 0) {
        if (n & 1) res = res * x;
        x = x * x;
        n >>= 1;
    }
    return res;
}

template <class T>
T mod_pow(T x, i64 n, i64 mod) {
    T res = 1;
    while (n > 0) {
        if (n & 1) res = (res * x) % mod;
        x = (x * x) % mod;
        n >>= 1;
    }
    return res;
}

template <class T>
T scan() {
    T val;
    std::cin >> val;
    return val;
}

template <class T>
struct Edge {
    int to;
    T cost;
    Edge(int _to, T _cost = 1) : to(_to), cost(_cost) {}
};

template <class T>
struct Graph : std::vector<std::vector<Edge<T>>> {
    using std::vector<std::vector<Edge<T>>>::vector;
    void add_edge(int u, int v, T w, bool directed = false) {
        (*this)[u].emplace_back(v, w);
        if (directed) return;
        (*this)[v].emplace_back(u, w);
    }
};

struct graph : std::vector<std::vector<int>> {
    using std::vector<std::vector<int>>::vector;
    void add_edge(int u, int v, bool directed = false) {
        (*this)[u].emplace_back(v);
        if (directed) return;
        (*this)[v].emplace_back(u);
    }
};

constexpr i64 LNF = std::numeric_limits<i64>::max() / 4;

constexpr int INF = std::numeric_limits<int>::max() / 2;

const std::vector<int> dy = {1, 0, -1, 0, 1, 1, -1, -1};
const std::vector<int> dx = {0, 1, 0, -1, 1, -1, 1, -1};

}  // namespace ebi

namespace ebi {

void main_() {
    int n;
    std::cin >> n;
    std::vector<i64> c(n);
    std::cin >> c;
    int q;
    std::cin >> q;
    std::vector<std::tuple<int,i64,i64>> queries(q);
    for(auto &[t,x,d]: queries) {
        std::cin >> t >> x >> d;
        x--;
    }
    std::vector<i64> a(n, 0);
    for(auto [t,x,d]: queries) {
        if(t == 1) {
            a[x] += d;
        } 
        else {
            a[x] -= d;
        }
        if(0 <= a[x] && a[x] <= c[x]) continue;
        std::cout << x+1 << '\n';
        return;
    }
    std::cout << "0\n";
}

}  // namespace ebi

int main() {
    std::cout << std::fixed << std::setprecision(15);
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int t = 1;
    // std::cin >> t;
    while (t--) {
        ebi::main_();
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5448
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin >> n;
    vector <int>c(n,0);
    vector <int>apple(n,0);
    for (int i = 0; i < n; i++)
    {
       cin>>c[i]; 
    }
    int q,ans;
    bool ok = true;
    cin >> q;
    for (int i = 0; i < q; i++)
    {
        int t,x,d;
        cin >> t >> x >> d;
        if (t==1 and ok)
        {
           if (apple[x-1]+d > c[x-1])
           {
               ans = x;
               ok=false;
           }

           else
           {
             apple[x-1]+=d;
           }
            
        }
        else
        {
           if (t==2 and ok)
           {
             if (apple[x-1] < d)
             {
                ans=x;
                ok=false;
             }
             else
             {
                apple[x-1]-=d;
             }
             
             
           }
           
        }   
    }
    if (ok)
    {
       cout<< 0 <<endl;
    }
    else
    {
       cout<<ans<<endl;
    }
    
    
    
    
}
accuracy: 1
time_ms: 50
memory_kb: 3112
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pll = pair<ll, ll>;
using uint = unsigned;
using pii = pair<int, int>;
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;

const int IINF = 0x1fffffff;
const ll LINF = 0x1fffffffffffffff;
const ll MINF = 0x7fffffffffff;
const ll MOD = 1000000007;
const ll MODD = 998244353;
const ld DINF=numeric_limits<ld>::infinity();
const ld EPS=1e-9;
const ld PI=3.1415926535897932;
const ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const ll dy[] = {1, 0, -1, 0, 1, 1, -1, -1};

#define overload4(_1,_2,_3,_4,name,...) name
#define overload3(_1,_2,_3,name,...) name
#define rep1(n) for(ll i=0;i<n;++i)
#define rep2(i,n) for(ll i=0;i<n;++i)
#define rep3(i,a,b) for(ll i=a;i<b;++i)
#define rep4(i,a,b,c) for(ll i=a;i<b;i+=c)
#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep1(n) for(ll i=n;i--;)
#define rrep2(i,n) for(ll i=n;i--;)
#define rrep3(i,a,b) for(ll i=b;i-->(a);)
#define rrep4(i,a,b,c) for(ll i=(a)+((b)-(a)-1)/(c)*(c);i>=(a);i-=c)
#define rrep(...) overload4(__VA_ARGS__,rrep4,rrep3,rrep2,rrep1)(__VA_ARGS__)
#define each1(i,a) for(auto&&i:a)
#define each2(x,y,a) for(auto&&[x,y]:a)
#define each3(x,y,z,a) for(auto&&[x,y,z]:a)
#define each(...) overload4(__VA_ARGS__,each3,each2,each1)(__VA_ARGS__)
#define all1(i) begin(i),end(i)
#define all2(i,a) begin(i),begin(i)+a
#define all3(i,a,b) begin(i)+a,begin(i)+b
#define all(...) overload3(__VA_ARGS__,all3,all2,all1)(__VA_ARGS__)
#define rall1(i) (i).rbegin(),(i).rend()
#define rall2(i,k) (i).rbegin(),(i).rbegin()+k
#define rall3(i,a,b) (i).rbegin()+a,(i).rbegin()+b
#define rall(...) overload3(__VA_ARGS__,rall3,rall2,rall1)(__VA_ARGS__)
// #define sum(...) accumulate(all(__VA_ARGS__),0LL)
// #define dsum(...) accumulate(all(__VA_ARGS__),0.0L)
// #define Msum(...) accumulate(all(__VA_ARGS__),0_M)
#define elif else if
#define unless(a) if(!(a))
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__;in(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;in(__VA_ARGS__)
#define Sort(a) sort(all(a))
#define Rev(a) reverse(all(a))
#define Uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))
#define vec vector
#define VEC(type,name,size) vector<type>name(size);in(name)
#define vv(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define VV(type,name,h,w) vector<vector<type>>name(h,vector<type>(w));in(name)
#define vvv(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
template<class T> auto min(const T& a){ return *min_element(all(a)); }
template<class T> auto max(const T& a){ return *max_element(all(a)); }
inline ll popcnt(ull a){ return __builtin_popcountll(a); }
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
template<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }
template<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }
template<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }
template<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }
vector<ll> iota(ll n, ll begin = 0){ vector<ll> a(n); iota(a.begin(), a.end(), begin); return a; }
vector<pll> factor(ull x){ vector<pll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; }
map<ll,ll> factor_map(ull x){ map<ll,ll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans[i] = 1; while((x /= i) % i == 0) ans[i]++; } if(x != 1) ans[x] = 1; return ans; }
vector<ll> divisor(ull x){ vector<ll> ans; for(ull i = 1; i * i <= x; i++) if(x % i == 0) ans.push_back(i); rrep(ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]); return ans; }
template<class T> unordered_map<T, ll> press(vector<T> a){ Uniq(a); unordered_map<T, ll> ans; rep(a.size()) ans[a[i]] = i; return ans; }
template<class T> map<T, ll> press_map(vector<T> a){ Uniq(a); map<T, ll> ans; rep(a.size()) ans[a[i]] = i; return ans; }
int scan(){ return getchar(); }
void scan(int& a){ scanf("%d", &a); }
void scan(unsigned& a){ scanf("%u", &a); }
void scan(long& a){ scanf("%ld", &a); }
void scan(long long& a){ scanf("%lld", &a); }
void scan(unsigned long long& a){ scanf("%llu", &a); }
void scan(char& a){ do{ a = getchar(); }while(a == ' ' || a == '\n'); }
void scan(float& a){ scanf("%f", &a); }
void scan(double& a){ scanf("%lf", &a); }
void scan(long double& a){ scanf("%Lf", &a); }
void scan(vector<bool>& a){ for(unsigned i = 0; i < a.size(); i++){ int b; scan(b); a[i] = b; } }
void scan(char a[]){ scanf("%s", a); }
void scan(string& a){ cin >> a; }
template<class T> void scan(vector<T>&);
template<class T, size_t size> void scan(array<T, size>&);
template<class T, class L> void scan(pair<T, L>&);
template<class T, size_t size> void scan(T(&)[size]);
template<class T> void scan(vector<T>& a){ for(auto&& i : a) scan(i); }
template<class T> void scan(deque<T>& a){ for(auto&& i : a) scan(i); }
template<class T, size_t size> void scan(array<T, size>& a){ for(auto&& i : a) scan(i); }
template<class T, class L> void scan(pair<T, L>& p){ scan(p.first); scan(p.second); }
template<class T, size_t size> void scan(T (&a)[size]){ for(auto&& i : a) scan(i); }
template<class T> void scan(T& a){ cin >> a; }
void in(){}
template <class... T> void in(T&... a){ (void)initializer_list<int>{ (scan(a), 0)... }; }
void print(){ putchar(' '); }
void print(bool a){ printf("%d", a); }
void print(int a){ printf("%d", a); }
void print(unsigned a){ printf("%u", a); }
void print(long a){ printf("%ld", a); }
void print(long long a){ printf("%lld", a); }
void print(unsigned long long a){ printf("%llu", a); }
void print(char a){ printf("%c", a); }
void print(char a[]){ printf("%s", a); }
void print(const char a[]){ printf("%s", a); }
void print(float a){ printf("%.15f", a); }
void print(double a){ printf("%.15f", a); }
void print(long double a){ printf("%.15Lf", a); }
void print(const string& a){ for(auto&& i : a) print(i); }
template<class T> void print(const complex<T>& a){ if(a.real() >= 0) print('+'); print(a.real()); if(a.imag() >= 0) print('+'); print(a.imag()); print('i'); }
template<class T> void print(const vector<T>&);
template<class T, size_t size> void print(const array<T, size>&);
template<class T, class L> void print(const pair<T, L>& p);
template<class T, size_t size> void print(const T (&)[size]);
template<class T> void print(const vector<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } }
template<class T> void print(const deque<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } }
template<class T, size_t size> void print(const array<T, size>& a){ print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } }
template<class T, class L> void print(const pair<T, L>& p){ print(p.first); putchar(' '); print(p.second); }
template<class T, size_t size> void print(const T (&a)[size]){ print(a[0]); for(auto i = a; ++i != end(a); ){ putchar(' '); print(*i); } }
template<class T> void print(const T& a){ cout << a; }
int out(){ putchar('\n'); return 0; }
template<class T> int out(const T& t){ print(t); putchar('\n'); return 0; }
template<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; }
#ifdef DEBUG
inline ll __lg(ull x){ return 63 - __builtin_clzll(x); }
#define debug(...) { print(#__VA_ARGS__); print(":"); out(__VA_ARGS__); }
#else
#define debug(...) void(0)
#endif
int first(bool i = true){ return out(i?"first":"second"); }
int First(bool i = true){ return out(i?"First":"Second"); }
int yes(bool i = true){ return out(i?"yes":"no"); }
int Yes(bool i = true){ return out(i?"Yes":"No"); }
int No(){ return out("No"); }
int YES(bool i = true){ return out(i?"YES":"NO"); }
int NO(){ return out("NO"); }
int Yay(bool i = true){ return out(i?"Yay!":":("); }
int possible(bool i = true){ return out(i?"possible":"impossible"); }
int Possible(bool i = true){ return out(i?"Possible":"Impossible"); }
int POSSIBLE(bool i = true){ return out(i?"POSSIBLE":"IMPOSSIBLE"); }
void Case(ll i){ printf("Case #%lld: ", i); }

#define fi first
#define se second
#define rng(a) a.begin(), a.end()
#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))
#define removeerase(x) x.erase(remove(rng(x), 0), x.end());
void debug_out() { std::cerr << std::endl; }
template <typename Head, typename... Tail> void debug_out(Head h, Tail... t) {
  std::cerr << " " << h;
  if (sizeof...(t) > 0)
    std::cout << " :";
  debug_out(t...);
}

constexpr uint mod = MOD;
struct Modint {
  uint num = 0;
  constexpr Modint() noexcept {}
  constexpr Modint(const Modint &x) noexcept : num(x.num) {}
  inline constexpr operator ll() const noexcept { return num; }
  inline constexpr Modint& operator+=(Modint x) noexcept {num += x.num; if (num >= mod) num -= mod; return *this; }
  inline constexpr Modint& operator-=(Modint x) noexcept { if (num < x.num) num += mod; num -= x.num; return *this; }
  inline constexpr Modint& operator*=(Modint x) noexcept { num = ull(num) * x.num % mod; return *this; }
  inline constexpr Modint& operator/=(Modint x) noexcept { return operator*=(x.inv()); }
  inline constexpr Modint& operator++() noexcept { if(num == mod - 1) num = 0; else num++; return *this; }
  inline constexpr Modint operator++(int) noexcept { Modint ans(*this); operator++(); return ans; }
  inline constexpr Modint& operator--() noexcept { if(num == 0) num = mod - 1; else num--; return *this; }
  inline constexpr Modint operator--(int) noexcept { Modint ans(*this); operator--(); return ans; }
  inline constexpr Modint operator-() const noexcept { return Modint(0) -= *this; }
  template<class T> constexpr Modint(T x) noexcept {
    using U = typename conditional<sizeof(T) >= 4, T, int>::type;
    U y = x; y %= U(mod); if(y < 0) y += mod; num = uint(y);
  }
  template<class T> inline constexpr Modint operator+(T x) const noexcept { return Modint(*this) += x; }
  template<class T> inline constexpr Modint& operator+=(T x) noexcept { return operator+=(Modint(x)); }
  template<class T> inline constexpr Modint operator-(T x) const noexcept { return Modint(*this) -= x; }
  template<class T> inline constexpr Modint& operator-=(T x) noexcept { return operator-=(Modint(x)); }
  template<class T> inline constexpr Modint operator*(T x) const noexcept { return Modint(*this) *= x; }
  template<class T> inline constexpr Modint& operator*=(T x) noexcept { return operator*=(Modint(x)); }
  template<class T> inline constexpr Modint operator/(T x) const noexcept { return Modint(*this) /= x; }
  template<class T> inline constexpr Modint& operator/=(T x) noexcept { return operator/=(Modint(x)); }
  inline constexpr Modint inv() const noexcept { ll x = 0, y = 0; extgcd(num, mod, x, y); return x; }
  static inline constexpr ll extgcd(ll a, ll b, ll &x, ll &y) noexcept { ll g = a; x = 1; y = 0; if(b){ g = extgcd(b, a % b, y, x); y -= a / b * x; } return g; }
  inline constexpr Modint pow(ull x) const noexcept { Modint ans = 1, cnt = *this; while(x){ if(x & 1) ans *= cnt; cnt *= cnt; x /= 2; } return ans; }
};
std::istream& operator>>(std::istream& is, Modint& x){ ll a; in(a); x = a; return is; }
inline constexpr Modint operator""_M(ull x) noexcept { return Modint(x); }
std::vector<Modint> fac(1, 1), inv(1, 1);
inline void reserve(ll a){
    if(fac.size() >= a) return;
    if(a < fac.size() * 2) a = fac.size() * 2;
    if(a >= mod) a = mod;
    while(fac.size() < a) fac.push_back(fac.back() * Modint(fac.size()));
    inv.resize(fac.size());
    inv.back() = fac.back().inv();
    for(ll i = inv.size() - 1; !inv[i - 1]; i--) inv[i - 1] = inv[i] * i;
}
inline Modint fact(ll n){ if(n < 0) return 0; reserve(n + 1); return fac[n]; }
inline Modint perm(ll n, ll r){
    if(r < 0 || n < r) return 0;
    if(n >> 24){ Modint ans = 1; for(ll i = 0; i < r; i++) ans *= n--; return ans; }
    reserve(n + 1); return fac[n] * inv[n - r];
}
inline Modint comb(ll n, ll r){ if(r < 0 || n < r) return 0; reserve(r + 1); return perm(n, r) * inv[r]; }
inline Modint Mcomb(ll n, ll r){ return comb(n + r - 1, n - 1); } // r balls into n boxes
inline Modint catalan(ll n){ reserve(n * 2 + 1); return fac[n * 2] * inv[n] * inv[n + 1]; }

const ll mx = 1 << 6;
struct Matrix{
    Modint a[mx][mx] = {};
    auto operator[](ll x){ return a[x]; }
    auto operator[](ll x) const { return a[x]; }
    static Matrix E(){
        Matrix a;
        for(ll i = 0; i < mx; i++) a[i][i] = 1;
        return a;
    }
    Matrix operator*(const Matrix& b) const {
        Matrix c;
        for(ll i = 0; i < mx; i++) for(ll j = 0; j < mx; j++) for(ll k = 0; k < mx; k++) c[i][k] += a[i][j] * b[j][k];
        return c;
    }
    void operator*=(const Matrix& b){
        *this = *this * b;
    }
    Matrix pow(ll b) const {
        Matrix a = *this, c = E();
        while(b){
            if(b & 1) c *= a;
            if(b >>= 1) a *= a;
        }
        return c;
    }
};

// vector<ll> facmod;
// vector<ll> facinvmod;
// ll combination(ll n, ll k, ll mod) {
//   // 階乗やその逆元をメモ化すると速い
//   // でかいやつはnCk/2^Nをパスカルの三角形で作る
//   ll res = fac(n, mod);
//   res = (res * invmod(fac(k, mod), mod)) % mod;
//   res = (res * invmod(fac(n - k, mod), mod)) % mod;
//   return res;
// }
// x % m[i] = r[i] % m[i] を満たす正で最小の x を返す
// i!=j に対して gcd(m[i], m[j])=1 であると仮定
// とりあえず解の存在判定は保留
// ll garner(vector<ll> r, vector<ll> m) {
//   int n = r.size();
//   ll m_prod = 1;      // m_prod には m[i] の積を入れていく
//   ll x = r[0] % m[0]; // 最初の条件を満たすような x の初期値
//   for (int i = 1; i < n; i++) {
//     // cout << r[i-1] << " " << m[i-1] << " " << x << endl;
//     m_prod *= m[i - 1]; // m の累積積を更新
//     ll t = ((r[i] - x) * invmod(m_prod, m[i])) % m[i];
//     if (t < 0)
//       t += m[i];
//     x += t * m_prod; // x を更新
//   }
//   return x;
// }
// false: composite, true: probably prime
// bool MillarRabin_sub(ll n, ll random) {
// 	ll d = n-1;
// 	while(d % 2 == 0) {
// // 		d /= 2;
// 		cout << random << " " << powmod(random, d, n) << endl;
// 		if (powmod(random, d, n) == n-1) {
// 			return true;
// 		}
// 	}
// 	if (powmod(random, d, n) == 1) return true;
// 	return false;
// }
// bool MillarRabin(ll n) {
// 	random_device rd{};
// 	vector<uint32_t> vec(20);
// 	generate(vec.begin(), vec.end(), ref(rd));
// 	mt19937 mt(seed_seq(vec.begin(), vec.end()));
// 	bool ret = true;
// 	rep(i,0,20) {
// 		vec[i] %= n; if (vec[i] == 0) continue;
// 		cout << MillarRabin_sub(n, random) << endl;
// 		if (!MillarRabin_sub(n, random)) return false;
// 	}
// 	return true;
// }

// 逆順になる reverse(rng(ans));
// vector<ll> G[100000];
// cout << fixed << setprecision(15) << y << endl;
// cout << bitset<20>(i) << endl;
// fill(distance[0], distance[row], INF);
// __builtin_popcount()
// stoi(S[j], 0, 2)
// for (ll i = 0;i < (ll)s.size() - k;i++)
// ll dp[桁数][未満フラグ][条件を満たしているか]
// stoi stoll
// bit全探索 n進数全探索
// while (t > 0) cc += t % 9, t /= 9;
// priority_queue<ll> que; que.push(3); que.top(); que.pop();
// rep(i,C)rep(j,C) if(i != j) rep(k,C) if(i != k && j != k)
// 始点終点をデータに含ませる
// memory restriction RE -> CE
// forr 逆順ループsnippet
// 座標 上下の回数さえ分かれば組合せ問題
// set 順序付き集合 distance(set.begin(), itr)でオフセットがわかる
// set s : for(int x : s) for(int y : s) for(int z : s)
// const int di[4] = {-1, 0, 1, 0};
// const int dj[4] = {0, -1, 0, 1};
// const string let = "ULDR";
// 逆順ソートsort(cand.rbegin(), cand.rend());
// int ok = 0, ng = 1e9+1;
// auto check = [&](int x) -> bool {
// 	set<int> s;
// 	rep(i,0,N) {
// 		int tmp = 0;
// 		rep(j,0,5) {
// 			if (A[i][j] >= x) tmp |= 1 << j;
// 		}
// 		s.insert(tmp);
// 	}
// 	for (int x : s) for (int y : s) for (int z : s) {
// 		if ((x | y | z) == 31) return true;
// 	}
// 	return false;
// };

// while(abs(ok - ng) > 1) {
// 	int mid = (ok + ng) / 2;
// 	if (check(mid)) ok = mid;
// 	else ng = mid;
// }
// cout << ok << endl;
// dp[500][500] はllではなくintにすること
// chmin/chmaxは型をあわせないといけない
// priority_queue 最上位以外アクセス許可していない
// priority_queue<l_l, vector<l_l>, greater<l_l>>
// multiset イテレータ書き込み出来ない

class FordFulkerson {
	struct edge {
		ll to, cap, rev;
	};
	ll n;
	vector<vector<edge>> G;
	vector<bool> used;

	ll dfs(ll s, ll t, ll f) {
		if (s == t) return f;
		used[s] = true;
		for (edge &e : G[s]) {
			if (!used[e.to] && e.cap > 0) {
				ll d = dfs(e.to, t, min(f, e.cap));
				if (d > 0) {
					e.cap -= d;
					G[e.to][e.rev].cap += d;
					return d;
				}
			}
		}
		return 0;
	}

public:
	FordFulkerson(ll n) : n(n), G(n), used(n) {}
	void add_edge(ll from, ll to, ll cap) {
		G[from].push_back({to, cap, ll(G[to].size())});
		G[to].push_back({from, 0, ll(G[from].size()) - 1});
	}

	ll max_flow(ll s, ll t) {
		ll flow = 0;
		while (true) {
			used.assign(n, 0);
			ll f = dfs(s, t, LINF);
			if (f == 0) return flow;
			flow += f;
		}
	}
};

int main() {
	ll N, Q;
	cin >> N;
	ll c[N];
	rep(i,0,N) cin >> c[i];
	cin >> Q;
	ll t[Q], x[Q], d[Q];
	rep(i,0,Q) {
		cin >> t[i] >> x[i] >> d[i];
		x[i]--;
	}

	ll v[N+1] = {};
	rep(i,0,Q) {
		if (t[i] == 1) {
			v[x[i]] += d[i];
			if (v[x[i]] > c[x[i]]) {
				cout << x[i] + 1 << endl;
				return 0;
			}
		}else{
			v[x[i]] -= d[i];
			if (v[x[i]] < 0) {
				cout << x[i] + 1 << endl;
				return 0;
			}
		}
	}
	cout << 0 << endl;
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 5812
final_rank: 16

code_snippet: #include <iostream>
#include <algorithm>
#include <utility>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <math.h>
#include <string.h>
#include <iomanip>
#include <numeric>
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <functional>
#include <limits>
#include <cassert>
#include <bitset>
#include <chrono>
#include <random>
#include <memory>
 
/* macro */
 
#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define rrep(i, a, n) for (int i = ((int)(n-1)); i >= (int)(a); i--)
#define Rep(i, a, n) for (i64 i = (i64)(a); i< (i64)(n); i++)
#define RRep(i, a, n) for (i64 i = ((i64)(n-i64(1))); i>=(i64)(a); i--)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
 
/* macro end */
 
/* template */
 
namespace ebi {
 
#ifdef LOCAL
#define debug(...)                                                    \
    std::cerr << "LINE: " << __LINE__ << "  [" << #__VA_ARGS__ << "]:", \
        debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
 
void debug_out() { std::cerr << std::endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
    std::cerr << " " << h;
    if (sizeof...(t) > 0) std::cout << " :";
    debug_out(t...);
}
 
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
    return os << pa.first << " " << pa.second;
}
 
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
    for (std::size_t i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : " ");
    return os;
}
 
using size_t = std::size_t;
using i64 = long long;
using u64 = unsigned long long;
 
template<class T> void fill(std::vector<T> &v) { for(T &e: v) std::cin >> e; }
 
template<class T>
struct Edge {
    int to;
    T cost;
    Edge(int to, T cost) : to(to), cost(cost) { }
};
 
template<class T>
using Graph = std::vector<std::vector<Edge<T>>>;
 
using graph = std::vector<std::vector<int>>;
 
template <class T>
inline bool chmin(T &a, T b) {
    if (a > b){
        a = b;
        return true;
    }
    return false;
}
 
template <class T>
inline bool chmax(T &a, T b) {
    if (a < b){
        a = b;
        return true;
    }
    return false;
}
 
template <class T>
T gcd(T a, T b){
    if( b==0 ) return a;
    else return gcd(b, a%b);
}
 
template <class T>
T lcm(T a, T b) {
    return (a*b)/gcd(a,b);
}
 
template<class T>
T Pow(T x, i64 n) {
    T res = 1;
    while(n>0){
        if(n&1) res = res *x;
        x = x*x;
        n>>=1;
    }
    return res;
}
 
template<class T>
T scan() {
    T val;
    std::cin >> val;
    return val;
}
 
constexpr i64 LNF = std::numeric_limits<i64>::max()/4;
 
constexpr int INF = std::numeric_limits<int>::max()/4;
 
const long double PI = acos(-1);
 
const std::vector<int> dy = {1,0,-1,0,1,1,-1,-1};
const std::vector<int> dx = {0,1,0,-1,1,-1,1,-1};
 
}
 
/* template end */

/*
    author: noshi91
    reference: https://noshi91.hatenablog.com/entry/2019/03/31/174006
    noshi91のブログで公開されているmodintを元にinv(), pow()を追加したものです
*/

#include <cstdint>
#include <iostream>

namespace ebi {

template<std::uint_fast64_t Modulus>
class modint {
  using u64 = std::uint_fast64_t;

public:
    u64 a;

    constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
    constexpr u64 &value() noexcept { return a; }
    constexpr u64 &val() noexcept { return a; }
    constexpr const u64 &value() const noexcept { return a; }
    constexpr modint operator+(const modint rhs) const noexcept {
        return modint(*this) += rhs;
    }
    constexpr modint operator-(const modint rhs) const noexcept {
        return modint(*this) -= rhs;
    }
    constexpr modint operator*(const modint rhs) const noexcept {
        return modint(*this) *= rhs;
    }
    constexpr modint operator/(const modint rhs) const noexcept {
        return modint(*this) /= rhs;
    }
    constexpr modint &operator+=(const modint rhs) noexcept {
        a += rhs.a;
        if (a >= Modulus) {
            a -= Modulus;
        }
        return *this;
    }
    constexpr modint &operator-=(const modint rhs) noexcept {
        if (a < rhs.a) {
        a += Modulus;
        }
        a -= rhs.a;
        return *this;
    }
    constexpr modint &operator*=(const modint rhs) noexcept {
        a = a * rhs.a % Modulus;
        return *this;
    }
    constexpr modint &operator/=(modint rhs) noexcept {
        u64 exp = Modulus - 2;
        while (exp) {
        if (exp % 2) {
            *this *= rhs;
        }
        rhs *= rhs;
        exp /= 2;
        }
        return *this;
    }
    constexpr modint operator-() const { return modint() - *this; }
    bool operator==(const u64 rhs) {
        return a == rhs;
    }
    bool operator!=(const u64 rhs) {
        return a != rhs;
    }
    constexpr modint& operator++() {
        a++;
        if( a == mod() ) a = 0;
        return *this;
    }
    constexpr modint& operator--() {
        if( a == 0 ) a = mod();
        a--;
        return *this;
    }

    modint pow(u64 n) const noexcept {
        modint res = 1;
        modint x = a;
        while(n>0){
            if(n&1) res *= x;
            x *= x;
            n >>=1;
        }
        return res;
    }
    modint inv() const {
        return pow(Modulus-2);
    }

    static u64 mod() {
        return Modulus;
    }
};

using modint998244353 = modint<998244353>;
using modint1000000007 = modint<1000000007>;

template<std::uint_fast64_t Modulus>
std::ostream& operator<<(std::ostream& os, modint<Modulus> a){
    return os << a.val();
}

} // namespace ebi

namespace ebi {

using mint = modint998244353;

using ld = long double;

void main_() {
    int n;
    std::cin >> n;
    std::vector<i64> c(n);
    fill(c);
    std::vector<i64> a(n,0);
    int q;
    std::cin >> q;
    std::vector in(q, std::vector<int>(3));
    rep(i,0,q) {
        fill(in[i]);
    }
    rep(i,1,q+1) {
        int t = in[i-1][0];
        int x = in[i-1][1] - 1;
        i64 d = in[i-1][2];
        if(t == 1) {
            if(a[x] + d > c[x]) {
                std::cout << x+1 << std::endl;
                return; 
            }
            a[x] += d;
        }
        else {
            if(a[x] - d < 0) {
                std::cout << x+1 << std::endl;
                return;
            }
            a[x] -= d;
        }
    }
    std::cout << 0 << std::endl;
}
 
}

 
int main() {
    std::cout << std::fixed << std::setprecision(15);
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int t = 1;
    while(t--) {
        ebi::main_();
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 8632
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uint = unsigned;
using pcc = pair<char, char>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tuplis = array<ll, 3>;
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
const ll LINF=0x1fffffffffffffff;
const ll MINF=0x7fffffffffff;
const int INF=0x3fffffff;
const int MOD=1000000007;
const int MODD=998244353;
const ld DINF=numeric_limits<ld>::infinity();
const ld EPS=1e-9;
const ld PI=3.1415926535897932;
const ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const ll dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
#define overload4(_1,_2,_3,_4,name,...) name
#define overload3(_1,_2,_3,name,...) name
#define rep1(n) for(ll i=0;i<n;++i)
#define rep2(i,n) for(ll i=0;i<n;++i)
#define rep3(i,a,b) for(ll i=a;i<b;++i)
#define rep4(i,a,b,c) for(ll i=a;i<b;i+=c)
#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep1(n) for(ll i=n;i--;)
#define rrep2(i,n) for(ll i=n;i--;)
#define rrep3(i,a,b) for(ll i=b;i-->(a);)
#define rrep4(i,a,b,c) for(ll i=(a)+((b)-(a)-1)/(c)*(c);i>=(a);i-=c)
#define rrep(...) overload4(__VA_ARGS__,rrep4,rrep3,rrep2,rrep1)(__VA_ARGS__)
#define each1(i,a) for(auto&&i:a)
#define each2(x,y,a) for(auto&&[x,y]:a)
#define each3(x,y,z,a) for(auto&&[x,y,z]:a)
#define each(...) overload4(__VA_ARGS__,each3,each2,each1)(__VA_ARGS__)
#define all1(i) begin(i),end(i)
#define all2(i,a) begin(i),begin(i)+a
#define all3(i,a,b) begin(i)+a,begin(i)+b
#define all(...) overload3(__VA_ARGS__,all3,all2,all1)(__VA_ARGS__)
#define rall1(i) (i).rbegin(),(i).rend()
#define rall2(i,k) (i).rbegin(),(i).rbegin()+k
#define rall3(i,a,b) (i).rbegin()+a,(i).rbegin()+b
#define rall(...) overload3(__VA_ARGS__,rall3,rall2,rall1)(__VA_ARGS__)
#define sum(...) accumulate(all(__VA_ARGS__),0LL)
#define dsum(...) accumulate(all(__VA_ARGS__),0.0L)
#define Msum(...) accumulate(all(__VA_ARGS__),0_M)
#define elif else if
#define unless(a) if(!(a))
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__;in(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;in(__VA_ARGS__)
#define Sort(a) sort(all(a))
#define Rev(a) reverse(all(a))
#define Uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))
#define vec(type,name,...) vector<type>name(__VA_ARGS__)
#define VEC(type,name,size) vector<type>name(size);in(name)
#define vv(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define VV(type,name,h,w) vector<vector<type>>name(h,vector<type>(w));in(name)
#define vvv(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
template<class T> auto min(const T& a){ return *min_element(all(a)); }
template<class T> auto max(const T& a){ return *max_element(all(a)); }
inline ll popcnt(ull a){ return __builtin_popcountll(a); }
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
template<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }
template<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }
template<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }
template<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }
vector<ll> iota(ll n, ll begin = 0){ vector<ll> a(n); iota(a.begin(), a.end(), begin); return a; }
vector<pll> factor(ull x){ vector<pll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; }
map<ll,ll> factor_map(ull x){ map<ll,ll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans[i] = 1; while((x /= i) % i == 0) ans[i]++; } if(x != 1) ans[x] = 1; return ans; }
vector<ll> divisor(ull x){ vector<ll> ans; for(ull i = 1; i * i <= x; i++) if(x % i == 0) ans.push_back(i); rrep(ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]); return ans; }
template<class T> unordered_map<T, ll> press(vector<T> a){ Uniq(a); unordered_map<T, ll> ans; rep(a.size()) ans[a[i]] = i; return ans; }
template<class T> map<T, ll> press_map(vector<T> a){ Uniq(a); map<T, ll> ans; rep(a.size()) ans[a[i]] = i; return ans; }
int scan(){ return getchar(); }
void scan(int& a){ scanf("%d", &a); }
void scan(unsigned& a){ scanf("%u", &a); }
void scan(long& a){ scanf("%ld", &a); }
void scan(long long& a){ scanf("%lld", &a); }
void scan(unsigned long long& a){ scanf("%llu", &a); }
void scan(char& a){ do{ a = getchar(); }while(a == ' ' || a == '\n'); }
void scan(float& a){ scanf("%f", &a); }
void scan(double& a){ scanf("%lf", &a); }
void scan(long double& a){ scanf("%Lf", &a); }
void scan(vector<bool>& a){ for(unsigned i = 0; i < a.size(); i++){ int b; scan(b); a[i] = b; } }
void scan(char a[]){ scanf("%s", a); }
void scan(string& a){ cin >> a; }
template<class T> void scan(vector<T>&);
template<class T, size_t size> void scan(array<T, size>&);
template<class T, class L> void scan(pair<T, L>&);
template<class T, size_t size> void scan(T(&)[size]);
template<class T> void scan(vector<T>& a){ for(auto&& i : a) scan(i); }
template<class T> void scan(deque<T>& a){ for(auto&& i : a) scan(i); }
template<class T, size_t size> void scan(array<T, size>& a){ for(auto&& i : a) scan(i); }
template<class T, class L> void scan(pair<T, L>& p){ scan(p.first); scan(p.second); }
template<class T, size_t size> void scan(T (&a)[size]){ for(auto&& i : a) scan(i); }
template<class T> void scan(T& a){ cin >> a; }
void in(){}
template <class... T> void in(T&... a){ (void)initializer_list<int>{ (scan(a), 0)... }; }
void print(){ putchar(' '); }
void print(bool a){ printf("%d", a); }
void print(int a){ printf("%d", a); }
void print(unsigned a){ printf("%u", a); }
void print(long a){ printf("%ld", a); }
void print(long long a){ printf("%lld", a); }
void print(unsigned long long a){ printf("%llu", a); }
void print(char a){ printf("%c", a); }
void print(char a[]){ printf("%s", a); }
void print(const char a[]){ printf("%s", a); }
void print(float a){ printf("%.15f", a); }
void print(double a){ printf("%.15f", a); }
void print(long double a){ printf("%.15Lf", a); }
void print(const string& a){ for(auto&& i : a) print(i); }
template<class T> void print(const complex<T>& a){ if(a.real() >= 0) print('+'); print(a.real()); if(a.imag() >= 0) print('+'); print(a.imag()); print('i'); }
template<class T> void print(const vector<T>&);
template<class T, size_t size> void print(const array<T, size>&);
template<class T, class L> void print(const pair<T, L>& p);
template<class T, size_t size> void print(const T (&)[size]);
template<class T> void print(const vector<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } }
template<class T> void print(const deque<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } }
template<class T, size_t size> void print(const array<T, size>& a){ print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } }
template<class T, class L> void print(const pair<T, L>& p){ print(p.first); putchar(' '); print(p.second); }
template<class T, size_t size> void print(const T (&a)[size]){ print(a[0]); for(auto i = a; ++i != end(a); ){ putchar(' '); print(*i); } }
template<class T> void print(const T& a){ cout << a; }
int out(){ putchar('\n'); return 0; }
template<class T> int out(const T& t){ print(t); putchar('\n'); return 0; }
template<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; }
#ifdef DEBUG
inline ll __lg(ull x){ return 63 - __builtin_clzll(x); }
#define debug(...) { print(#__VA_ARGS__); print(":"); out(__VA_ARGS__); }
#else
#define debug(...) void(0)
#endif
int first(bool i = true){ return out(i?"first":"second"); }
int First(bool i = true){ return out(i?"First":"Second"); }
int yes(bool i = true){ return out(i?"yes":"no"); }
int Yes(bool i = true){ return out(i?"Yes":"No"); }
int No(){ return out("No"); }
int YES(bool i = true){ return out(i?"YES":"NO"); }
int NO(){ return out("NO"); }
int Yay(bool i = true){ return out(i?"Yay!":":("); }
int possible(bool i = true){ return out(i?"possible":"impossible"); }
int Possible(bool i = true){ return out(i?"Possible":"Impossible"); }
int POSSIBLE(bool i = true){ return out(i?"POSSIBLE":"IMPOSSIBLE"); }
void Case(ll i){ printf("Case #%lld: ", i); }




int main(){
    LL(n);
    VEC(ll,c,n);
    vec(ll,a,n);
    LL(q);
    rep(q){
        LL(t,x,d);
        x--;
        if(t==1){
            a[x]+=d;
            if(a[x]>c[x])return out(x+1);
        }
        else{
            a[x]-=d;
            if(a[x]<0)return out(x+1);
        }
    }
    out(0);
}
accuracy: 1
time_ms: 10
memory_kb: 3588
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;


signed main(){
    ios::sync_with_stdio(false);
	cin.tie(0);
    cout << fixed << setprecision(20);

    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    int q;
    cin>>q;
    int c[n]={};
    while(q--){
        int t,x,d;
        cin>>t>>x>>d;
        x--;
        if(t==1){
            c[x] += d;
        }
        else c[x] -= d;
        if(c[x] > a[x] || c[x]<0){
            cout << x+1 << endl;
            return 0;
        }
    }
    cout << 0 << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3220
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;


signed main(){
    ios::sync_with_stdio(false);
	cin.tie(0);
    cout << fixed << setprecision(20);

    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    int q;
    cin>>q;
    int c[n]={};
    while(q--){
        int t,x,d;
        cin>>t>>x>>d;
        x--;
        if(t==1){
            c[x] += d;
        }
        else c[x] -= d;
        if(c[x] > a[x] || c[x]<0){
            cout << x+1 << endl;
            return 0;
        }
    }
    cout << 0 << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3188
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    vector<int> C(n);
    for(int i = 0; i < n; i++){
        cin >> C[i];
    }

    int q;
    cin >> q;
    vector<int> A(n, 0);
    while(q--){
        int t, x, d;
        cin >> t >> x >> d;
        x--;
        if(t == 1){
            A[x] += d;
            if(A[x] > C[x]){
                cout << x + 1 << endl;
                return 0;
            }
        }else{
            A[x] -= d;
            if(A[x] < 0){
                cout << x + 1 << endl;
                return 0;
            }
        }
    }

    cout << 0 << endl;

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3216
final_rank: 2

code_snippet: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <limits>
#include <cmath>
#include <cassert>

using namespace std;
using ll = long long;

const int INF = 1<<30;
const int MOD = (int)1e9 + 7;
const int MAX_N = (int)1e5 + 5;
#define debug(x) cout << #x << ": " << x << endl

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<int> c(n), cnt(n, 0);
    for(int i = 0; i < n; i++) cin >> c[i];
    int q; cin >> q;
    int ans = -1;
    for(int i = 0; i < q; i++)
    {
        int t, x, d; cin >> t >> x >> d;
        x--;
        if(t == 1)
        {
            cnt[x] += d;
            if(cnt[x] > c[x] and ans == -1) ans = x + 1;
        }
        else
        {
            cnt[x] -= d;
            if(cnt[x] < 0 and ans == -1) ans = x + 1;
        }
    }
    if(ans == -1) cout << 0 << endl;
    else cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3248
final_rank: 5

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef double ld;
typedef complex<ld> Point;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(iinclude<snt i=sta;i<=n;i++)
const ld eps = 1e-6;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;

int c[1000];
int a[1000];
void solve() {
	int n; cin >> n;
	rep(i, n)cin >> c[i];
	int q; cin >> q;
	rep(i, q) {
		int t; cin >> t; int x, d; cin >> x >> d;
		if (t == 1) {
			x--; a[x] += d;
			if (a[x] > c[x]) {
				cout << x + 1 << endl; return;
			}
		}
		else {
			x--; a[x] -= d;
			if (a[x] < 0) {
				cout << x + 1 << endl; return;
			}
		}
	}
	cout << 0 << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	//stop
		return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3224
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(),x.end()
#define dbg(x) cout<<#x<<":"<<x<<endl
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;

int main(){
  int n;
  cin>>n;
  vector<int> c(n+1),cnt(n+1,0);
  int ans=0;
  for(int i=0;i<n;i++){
    cin>>c[i+1];
  }
  int q;
  cin>>q;
  for(int i=0;i<q;i++){
    int t,x,d;
    cin>>t>>x>>d;
    if(t==1)cnt[x]+=d;
    else cnt[x]-=d;
    if((cnt[x]<0||c[x]<cnt[x])&&ans==0){
	 ans=x;
    }
  }
  cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3116
final_rank: 19