static int max_profit_unlimited_pairs(List<Integer> A) {
		if (A.size() <= 1) {
			return 0;
		}

		int profit = 0, buy = A.get(0);
		for (int i = 1; i < A.size() - 1; ++i) {
			if (A.get(i + 1) < A.get(i) && A.get(i - 1) <= A.get(i)) { // sell at local maximum.
				profit += A.get(i) - buy;
				buy = A.get(i + 1);
			} else if (A.get(i + 1) >= A.get(i) && A.get(i - 1) > A.get(i)) { // buy at local minimum
				buy = A.get(i);
			}
		}

		if (A.get(A.size() - 1) > buy) {
			profit += A.get(A.size() - 1) - buy;
		}
		return profit;
	}