Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func parseToArray2d(lines []string) [][]int64 {
	var result [][]int64
	for _, line := range lines {
		var parsedLine []int64
		for _, element := range strings.Split(line, " ") {
			n, err := strconv.ParseInt(element, 10, 64)
			if err != nil {
				fmt.Printf("Error: element = %v\n", element)
			}
			parsedLine = append(parsedLine, n)
		}
		result = append(result, parsedLine)
	}
	return result
}

func pop(stack []int64) ([]int64, int64) {
	lenStack := len(stack)
	return stack[0:(lenStack - 1)], stack[lenStack-1]
}

func contains(s []int64, e int64) bool {
	for _, a := range s {
		if a == e {
			return true
		}
	}
	return false
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	content := []string{}
	for scanner.Scan() {
		line := scanner.Text()
		content = append(content, line)
	}
	arr := parseToArray2d(content)
	input := arr[1]

	stack := []int64{}
	result := -1

	for i, x := range input {
		if x > 0 {
			if contains(stack, x) {
				result = i + 1
				break
			} else {
				stack = append(stack, x)
			}
		} else {
			var top int64
			if len(stack) == 0 {
				result = i + 1
				break
			}
			stack, top = pop(stack)
			if top != -x {
				result = i + 1
			}
		}
	}

	if result < 0 {
		fmt.Println("OK")
	} else {
		fmt.Println(result)
	}
}

C++: #include "bits/stdc++.h"
using namespace std;

#define ASC(vec) vec.begin(), vec.end()    // 昇順ソート 例：sort(ASC(vec));
#define DESC(vec) vec.rbegin(), vec.rend() // 降順ソート 例：sort(DESC(vec));
#define rep(i, n) for (int i = 0; i < (n); i++)
#define Rep(i, n) for (int i = 1; i < n; i++)
#define REP(i, vec) for (auto i = vec.begin(); i != vec.end(); ++i)

const int       mod = 1000000007;
const int       inf = (1 << 21);
const long long INF = 1LL << 60;

using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vb = vector<bool>;
using vl = vector<ll>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvb = vector<vector<bool>>;
using vii = vector<pair<int, int>>;
using vll = vector<pair<ll, ll>>;

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 / GCD(a, b) * b; }

constexpr array<int, 9> dx = { 0, 1, 0, -1, -1, 1, 1, -1, 0 };
constexpr array<int, 9> dy = { 1, 0, -1, 0, 1, 1, -1, -1, 0 };

// ──────────────────────────────────────────────────────────────────

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

	vi c(n);
	rep(i, n) cin >> c[i];
	
	stack<int> st;
	bool check[101] = {};

	rep(i, n) {
		if (c[i] > 0) {
			if (check[c[i]]) {
				cout << i + 1 << endl;
				return 0;
			}
			else {
				st.push(c[i]);
				check[c[i]] = true;
			}
		}
		else {
			if (!st.empty() && st.top() == -c[i]) {
				st.pop();
				check[-c[i]] = false;
			}
			else {
				cout << i + 1 << endl;
				return 0;
			}
		}
	}
	cout << "OK" << endl;
}

C++: #include <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <iomanip>


using namespace std;

#define REP(i, n) for(ll i = 0;i < n;i++)
#define REPR(i, n) for(ll i = n;i >= 0;i--)
#define FOR(i, m, n) for(ll i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define REPO(i, n) for(ll i = 1;i <= n;i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(),n.end()
#define MOD (ll)1000000007
#define P pair<ll, ll>

stack<ll> s;
ll n;
bool used[1100], ok = true;
int main() {
	cin >> n;
	REP(i, n) {
		ll a;
		cin >> a;
		if (a > 0) {
			if (used[a]) ok = false;
			else {
				s.push(a);
				used[a] = true;
			}
		}
		else {
			if (s.empty() or s.top() != a * -1)ok = false;
			else {
				s.pop();
				used[a * -1] = false;
			}
		}
		if (!ok) {
			cout << i + 1 << endl;
			return 0;
		}
	}
	cout << "OK" << endl;
}

C++: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 105

bool check[SIZE];

int main(){

	int L;
	scanf("%d",&L);

	stack<int> S;

	for(int i = 1; i <= 100; i++){

		check[i] = false;
	}

	int tmp,ans;
	bool FLG = true;

	for(int i = 1; i <= L; i++){

		scanf("%d",&tmp);

		if(!FLG)continue;

		if(tmp < 0){ //猫出

			if(S.empty() == true || S.top() != abs(tmp)){

				FLG = false;
				ans = i;

			}else{

				S.pop();
				check[abs(tmp)] = false;
			}

		}else{ //猫入

			if(check[tmp]){

				FLG = false;
				ans = i;
			}else{

				S.push(tmp);
				check[tmp] = true;
			}
		}
	}

	if(FLG){

		printf("OK\n");

	}else{

		printf("%d\n",ans);
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>
#include<complex>

using namespace std;

#define ll long long
#define ld long double
#define EPS 1e-9
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef pair<pii,int> ppi;
typedef pair<int,pii> pip;
typedef vector<pii> vp;
typedef vector<vi> vvi;

int gcd(int a, int b){if(b==0) return a;return gcd(b,a%b);}
int lcm(int a, int b){return a/gcd(a,b)*b;}


signed main(void) {
    int n;
    cin >> n;
    vi a(n);
    rep(i,n)cin >> a[i];
    vi in(1000,0);
    stack<int> s;
    rep(i,n){
        if(a[i] > 0){//いまから入る
            if(in[a[i]]){
                cout << i+1 << endl;
                return 0;
            }
            in[a[i]]++;
            s.push(a[i]);
        }else{//いまから出る
            a[i] *= -1;
            if(s.empty() || s.top() != a[i]){
                cout << i+1 << endl;
                return 0;
            }
            s.pop();
            in[a[i]] = 0;
        }
    }
    cout << "OK" << endl;
}

C++: // 2020/07/20 Tazoe

#include <iostream>
using namespace std;

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

	int c[1001];			// 猫の情報
	for(int i=1; i<=L; i++){
		cin >> c[i];
	}

	int A[101];			// 猫のスタック位置
	for(int i=1; i<=100; i++){
		A[i] = -1;
	}
	int sp = 0;			// スタックポインタ

	for(int i=1; i<=L; i++){
		if(c[i]>0){				// 横穴に入った
			if(A[c[i]]==-1){
				A[c[i]] = sp;
				sp++;
			}
			else{
				cout << i << endl;
				return 0;
			}
		}
		else{					// 横穴から出た
			if(sp>0 && A[-c[i]]==sp-1){
				A[-c[i]] = -1;
				sp--;
			}
			else{
				cout << i << endl;
				return 0;
			}
		}
	}
	
	cout << "OK" << endl;
	return 0;
}

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

int main() {
    std::vector<bool> isin(101);
    std::vector<int> t;
    int n; std::cin >> n;
    for (int i = 0; i < n; ++i) {
        int a; std::cin >> a;
        if (a > 0) {
            if (isin[a]) {
                std::cout << i + 1 << std::endl;
                return 0;
            }
            isin[a] = true;
            t.push_back(a);
        } else {
            a = -a;
            if (!isin[a] || t.empty() || t.back() != a) {
                std::cout << i + 1 << std::endl;
                return 0;
            }
            isin[a] = false;
            t.pop_back();
        }
    }
    std::cout << "OK" << std::endl;
}