> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximize Y-Sum by Picking a Triplet of

> Tested Python solution for LeetCode 3572 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 3572, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Greedy](/catalog/topics/greedy), [Sorting](/catalog/topics/sorting), [Heap (Priority Queue)](/catalog/topics/heap-priority-queue). [View on LeetCode](https://leetcode.com/problems/maximize-ysum-by-picking-a-triplet-of-distinct-xvalues/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 3572   # by problem number
lcpy gen -s maximize_ysum_by_picking_a_triplet_of_distinct_xvalues   # by problem name
```

## Problem

You are given two integer arrays \<code>x\</code> and \<code>y\</code>, each of length \<code>n\</code>. You must choose three \<strong>distinct\</strong> indices \<code>i\</code>, \<code>j\</code>, and \<code>k\</code> such that:

\<ul>
\<li>\<code>x\[i] != x\[j]\</code>\</li>
\<li>\<code>x\[j] != x\[k]\</code>\</li>
\<li>\<code>x\[k] != x\[i]\</code>\</li>
\</ul>

Your goal is to \<strong>maximize\</strong> the value of \<code>y\[i] + y\[j] + y\[k]\</code> under these conditions. Return the \<strong>maximum\</strong> possible sum that can be obtained by choosing such a triplet of indices.

If no such triplet exists, return -1.

### Examples

```
Input: x = [1,2,1,3,2], y = [5,3,4,6,2]
Output: 14
Explanation: Choose i = 0 (x[i] = 1, y[i] = 5), j = 1 (x[j] = 2, y[j] = 3), k = 3 (x[k] = 3, y[k] = 6). All three values chosen from x are distinct. 5 + 3 + 6 = 14 is the maximum we can obtain.
```

```
Input: x = [1,2,1,2], y = [4,5,6,7]
Output: -1
Explanation: There are only two distinct values in x. Hence, the output is -1.
```

### Constraints

* n == x.length == y.length
* 3 \<= n \<= 10^5
* 1 \<= x\[i], y\[i] \<= 10^6

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximize_ysum_by_picking_a_triplet_of_distinct_xvalues/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximize_ysum_by_picking_a_triplet_of_distinct_xvalues/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def max_sum_distinct_triplet(self, x: list[int], y: list[int]) -> int:
        best: dict[int, int] = {}
        for xi, yi in zip(x, y, strict=True):
            if yi > best.get(xi, 0):
                best[xi] = yi
        if len(best) < 3:
            return -1
        return sum(sorted(best.values(), reverse=True)[:3])
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.