# Swap and Maximise

Problem Link: <https://www.codingninjas.com/codestudio/problems/swap-and-maximise_1164405?leftPanelTab=0>

```python
def maxSum(arr: list, n: int):
    # Write your code here.
    arr.sort()
    newarr=[]
    i=0
    j=len(arr)-1
    while(i<=j):
        newarr.append(arr[i])
        i+=1
        if(i<=j):
            newarr.append(arr[j])
            j-=1
    ans=0
    for i in range(n-1):
        ans=ans+abs(newarr[i+1]-newarr[i])
    ans=ans+abs(newarr[0]-newarr[n-1])
    return ans
```
