Source Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void solve() {
	int n, m, a[1002] = {};
	vector<int>v;
	cin >> n >> m;
	int x;
	for (int i = 0; i < n; i++) {
		cin >> x;
		if (a[x] == 0) {
			v.push_back(x);
		}
		a[x]++;
	}
	int sum = 0;
	int z = v.size();
	while (m--) {
		cin >> x;
		if (v[z - 1] < x) {
			v.push_back(x);
			z++;
			a[x]++;
		}
		else if (v[z - 1] > x) {
			for (int i = 0; i < z; i++) {
				if (v[i] > x) {
					sum += (z - i);
					if (a[x] == 0) {
						z++;
						v.push_back(x);
						sort(v.begin(), v.end());
					}
					a[x]++;
					break;
				}
			}
		}
	}
	cout << sum;
}

int main() {
	int t = 1;
	//cin >> t;
	while(t--){
		solve();
	}
}
Copy
Insertion sort. No not that one lafi-Odeh
GNU G++17
15 ms
936 KB
Accepted