text:
# helper function for Nikkhoo's dislocation Green's func and
# Barbot's volume ones

@inline function coord_trans(x1::T, x2::T, x3::T, A::M) where {T<:Number, M<:AbstractMatrix}
    r1 = A[1,1] * x1 + A[1,2] * x2 + A[1,3] * x3
    r2 = A[2,1] * x1 + A[2,2] * x2 + A[2,3] * x3
    r3 = A[3,1] * x1 + A[3,2] * x2 + A[3,3] * x3
    r1, r2, r3
end

# @inline function coord_trans(x1::T, x2::T, x3::T, A::M) where {T<:AbstractVector, M<:AbstractMatrix}
#     r1, r2, r3 = similar(x1), similar(x2), similar(x3)
#     @inbounds @simd for i ∈ eachindex(r1)
#         r1[i] = A[1,1] * x1[i] + A[1,2] * x2[i] + A[1,3] * x3[i]
#         r2[i] = A[2,1] * x1[i] + A[2,2] * x2[i] + A[2,3] * x3[i]
#         r3[i] = A[3,1] * x1[i] + A[3,2] * x2[i] + A[3,3] * x3[i]
#     end
#     r1, r2, r3
# end

@inline function AngDisDisp(x::U, y::U, z::U, alpha::T, bx::T, by::T, bz::T, ν::T) where {T<:Number, U}
    sinA, cosA = sincos(alpha)
    eta = @. y * cosA - z * sinA
    zeta = @. y * sinA + z * cosA
    r = hypot.(x, y, z)

    if U <: Number
        zeta = ifelse(zeta > r, r, zeta)
        _z = ifelse(z > r, r, z)
    elseif U <: AbstractArray
        bool = zeta .> r
        zeta[bool] .= r[bool]
        @. bool = z > r
        _z = deepcopy(z)
        _z[bool] .= r[bool]
    end

    ux = @. bx / 8 / π / (1 - ν) * (x * y / r / (r - _z) - x * eta / r / (r - zeta))
    vx = @. bx / 8 / π / (1 - ν) * (eta * sinA / (r - zeta) - y * eta / r / (r - zeta) + y ^ 2 / r / (r - _z) + (1 - 2 * ν) * (cosA * log(r - zeta) - log(r - _z)))
    wx = @. bx / 8 / π / (1 - ν) * (eta * cosA / (r - zeta) - y / r - eta * _z / r / (r - zeta) - (1 - 2 * ν) * sinA * log(r - zeta))

    uy = @. by / 8 / π / (1 - ν) * (x ^ 2 * cosA / r / (r - zeta) - x ^ 2 / r / (r - _z) - (1 - 2 * ν) * (cosA * log(r - zeta) - log(r - _z)))
    vy = @. by * x / 8 / π / (1 - ν) * (y * cosA / r / (r - zeta) - sinA * cosA / (r - zeta) - y / r / (r - _z))
    wy = @. by * x / 8 / π / (1 - ν) * (_z * cosA / r / (r - zeta) - cosA ^ 2 / (r - zeta) + 1 / r)

    uz = @. bz * sinA / 8 / π / (1 - ν) * ((1 - 2 * ν) * log(r - zeta) - x ^ 2 / r / (r - zeta))
    vz = @. bz * x * sinA / 8 / π / (1 - ν) * (sinA / (r - zeta) - y / r / (r - zeta))
    wz = @. bz * x * sinA / 8 / π / (1 - ν) * (cosA / (r - zeta) - _z / r / (r - zeta))

    return ux + uy + uz, vx + vy + vz, wx + wy + wz
end

@inline function AngDisStrain(x::U, y::U, z::U, alpha::T, bx::T, by::T, bz::T, ν::T) where {T, U}
    sinA, cosA = sincos(alpha)
    eta = @. y * cosA - z * sinA
    zeta = @. y * sinA + z * cosA
    x2 = @. x ^ 2
    y2 = @. y ^ 2
    z2 = @. z ^ 2
    r2 = @. x2 + y2 + z2
    r = @. √(r2) # use `hypot` for higher precision
    r3 = @. r2 * r
    rz = @. r * (r - z)
    r2z2 = @. rz ^ 2
    r3z = @. r3 * (r - z)
    W = @. zeta - r
    W2 = @. W ^ 2
    Wr = @. W * r
    W2r = @. W2 * r
    Wr3 = @. W * r3
    W2r2 = @. W2 * r2
    C = @. (r * cosA - z) / Wr
    S = @. (r * sinA - y) / Wr

    rFi_rx = @. (eta / r / (r - zeta) - y / r / (r - z)) / 4 / π
    rFi_ry = @. (x / r / (r - z) - cosA * x / r / (r - zeta)) / 4 / π
    rFi_rz = @. (sinA * x / r / (r - zeta)) / 4 / π

    Exx = @. (bx * (rFi_rx) + bx / 8 / π / (1 - ν) * (eta / Wr + eta * x2 / W2r2 - eta * x2 / Wr3 + y / rz -
        x2 * y / r2z2 - x2 * y / r3z) - by * x / 8 / π / (1 - ν) * (((2 * ν + 1) / Wr + x2 / W2r2 - x2 / Wr3) * cosA +
        (2 * ν + 1) / rz - x2 / r2z2 - x2 / r3z) + bz * x * sinA / 8 / π / (1 - ν) * ((2 * ν + 1) / Wr + x2 / W2r2 - x2 / Wr3))

    Eyy = @. (by * (rFi_ry) +
        bx / 8 / π / (1 - ν) * ((1 / Wr + S ^ 2 - y2 / Wr3) * eta + (2 * ν + 1) * y / rz - y ^ 3 / r2z2 -
        y ^ 3 / r3z - 2 * ν * cosA * S) -
        by * x / 8 / π / (1 - ν) * (1 / rz - y2 / r2z2 - y2 / r3z +
        (1 / Wr + S ^ 2 - y2 / Wr3) * cosA) +
        bz * x * sinA / 8 / π / (1 - ν) * (1 / Wr + S ^ 2 - y2 / Wr3))

    Ezz = @. (bz * (rFi_rz) +
        bx / 8 / π / (1 - ν) * (eta / W / r + eta * C ^ 2 - eta * z2 / Wr3 + y * z / r3 +
        2 * ν * sinA * C) -
        by * x / 8 / π / (1 - ν) * ((1 / Wr + C ^ 2 - z2 / Wr3) * cosA + z / r3) +
        bz * x * sinA / 8 / π / (1 - ν) * (1 / Wr + C ^ 2 - z2 / Wr3))

    Exy = @. (bx * (rFi_ry) / 2 + by * (rFi_rx) / 2 -
        bx / 8 / π / (1 - ν) * (x * y2 / r2z2 - ν * x / rz + x * y2 / r3z - ν * x * cosA / Wr +
        eta * x * S / Wr + eta * x * y / Wr3) +
        by / 8 / π / (1 - ν) * (x2 * y / r2z2 - ν * y / rz + x2 * y / r3z + ν * cosA * S +
        x2 * y * cosA / Wr3 + x2 * cosA * S / Wr) -
        bz * sinA / 8 / π / (1 - ν) * (ν * S + x2 * S / Wr + x2 * y / Wr3))

    Exz = @. (bx * (rFi_rz) / 2 + bz * (rFi_rx) / 2 -
        bx / 8 / π / (1 - ν) * (-x * y / r3 + ν * x * sinA / Wr + eta * x * C / Wr +
        eta * x * z / Wr3) +
        by / 8 / π / (1 - ν) * (-x2 / r3 + ν / r + ν * cosA * C + x2 * z * cosA / Wr3 +
        x2 * cosA * C / Wr) -
        bz * sinA / 8 / π / (1 - ν) * (ν * C + x2 * C / Wr + x2 * z / Wr3))

    Eyz = @. (by * (rFi_rz) / 2 + bz * (rFi_ry) / 2 +
        bx / 8 / π / (1 - ν) * (y2 / r3 - ν / r - ν * cosA * C + ν * sinA * S + eta * sinA * cosA / W2 -
        eta * (y * cosA + z * sinA) / W2r + eta * y * z / W2r2 - eta * y * z / Wr3) -
        by * x / 8 / π / (1 - ν) * (y / r3 + sinA * cosA ^ 2 / W2 - cosA * (y * cosA + z * sinA) /
        W2r + y * z * cosA / W2r2 - y * z * cosA / Wr3) -
        bz * x * sinA / 8 / π / (1 - ν) * (y * z / Wr3 - sinA * cosA / W2 + (y * cosA + z * sinA) /
        W2r - y * z / W2r2))

    return Exx, Eyy, Ezz, Exy, Exz, Eyz
end

@inline function TensTrans(Txx1::T, Tyy1::T, Tzz1::T, Txy1::T, Txz1::T, Tyz1::T, A::M) where {T, M}
    Txx2 = @. A[1] ^ 2 * Txx1 + 2 * A[1] * A[4] * Txy1 + 2 * A[1] * A[7] * Txz1 + 2 * A[4] * A[7] * Tyz1 + A[4] ^ 2 * Tyy1 + A[7] ^ 2 * Tzz1
    Tyy2 = @. A[2] ^ 2 * Txx1 + 2 * A[2] * A[5] * Txy1 + 2 * A[2] * A[8] * Txz1 + 2 * A[5] * A[8] * Tyz1 + A[5] ^ 2 * Tyy1 + A[8] ^ 2 * Tzz1
    Tzz2 = @. A[3] ^ 2 * Txx1 + 2 * A[3] * A[6] * Txy1 + 2 * A[3] * A[9] * Txz1 + 2 * A[6] * A[9] * Tyz1 + A[6] ^ 2 * Tyy1 + A[9] ^ 2 * Tzz1
    Txy2 = @. A[1] * A[2] * Txx1 + (A[1] * A[5] + A[2] * A[4]) * Txy1 + (A[1] * A[8] + A[2] * A[7])* Txz1 + (A[8] * A[4] + A[7] * A[5]) * Tyz1 + A[5] * A[4] * Tyy1 + A[7] * A[8] * Tzz1
    Txz2 = @. A[1] * A[3] * Txx1 + (A[1] * A[6] + A[3] * A[4]) * Txy1 + (A[1] * A[9] + A[3] * A[7])* Txz1 + (A[9] * A[4] + A[7] * A[6]) * Tyz1 + A[6] * A[4] * Tyy1 + A[7] * A[9] * Tzz1
    Tyz2 = @. A[2] * A[3] * Txx1 + (A[3] * A[5] + A[2] * A[6]) * Txy1 + (A[3] * A[8] + A[2] * A[9])* Txz1 + (A[8] * A[6] + A[9] * A[5]) * Tyz1 + A[5] * A[6] * Tyy1 + A[8] * A[9] * Tzz1
    return Txx2, Tyy2, Tzz2, Txy2, Txz2, Tyz2
end

heaviside(x::T) where T = ifelse(x ≤ zero(T), zero(T), one(T))

xlogy(x::T, y::T) where T = ifelse(isapprox(x, zero(T)), zero(T), x * log(y))

xlogy(x, y) = xlogy(promote(x, y)...)

omega(x::T) where T = heaviside(x + 1/2) - heaviside(x - 1/2)

S(x::T) where T = omega(x - 1/2)


text:
using Documenter
using OCReract

makedocs(;
    modules=[OCReract],
    format=Documenter.HTML(),
    pages=[
        "Home" => "index.md",
    ],
    repo="https://github.com/leferrad/OCReract.jl/blob/{commit}{path}#L{line}",
    sitename="OCReract.jl",
    authors="Leandro Ferrado"
)

deploydocs(;
    repo="github.com/leferrad/OCReract.jl",
)

ext: jl


text:
"""
	mapproject(cmd0::String="", arg1=[], kwargs...)

Forward and inverse map transformations, datum conversions and geodesy.

Full option list at [`mapproject`](http://gmt.soest.hawaii.edu/doc/latest/mapproject.html)

Parameters
----------

- $(GMT.opt_R)
- $(GMT.opt_J)

- **A** : **origin** : -- Str --    Flags = b|B|f|F|o|O[lon0/lat0][+v]

    Calculate azimuth along track or to the optional fixed point set with lon0/lat0.
    [`-A`](http://gmt.soest.hawaii.edu/doc/latest/mapproject.html#a)
- **C** : **center** : -- Str or list or [] --    Flags = [dx/dy]

    Set center of projected coordinates to be at map projection center [Default is lower left corner].
    [`-C`](http://gmt.soest.hawaii.edu/doc/latest/mapproject.html#c)
- **D** : **override_units** : -- Str --    Flags = c|i|p

    Temporarily override PROJ_LENGTH_UNIT and use c (cm), i (inch), or p (points) instead.
    [`-D`](http://gmt.soest.hawaii.edu/doc/latest/mapproject.html#d)
- **E** : **geod2ecef** : -- Str or [] --    Flags = [datum]

    Convert from geodetic (lon, lat, height) to Earth Centered Earth Fixed (ECEF) (x,y,z) coordinates.
    [`-E`](http://gmt.soest.hawaii.edu/doc/latest/mapproject.html#e)
- **F** : **one2one** : -- Str or [] --    Flags = [unit]

    Force 1:1 scaling, i.e., output (or input, see I) data are in actual projected meters.
    [`-F`](http://gmt.soest.hawaii.edu/doc/latest/mapproject.html#f)
- **G** : **track_distances** : -- Str or List --    Flags = [lon0/lat0][+a][+i][+u[+|-]unit][+v]

    Calculate distances along track or to the optional fixed point set with G="lon0/lat0".
    [`-G`](http://gmt.soest.hawaii.edu/doc/latest/mapproject.html#g)
- **L** : **dist2line** : -- Str --    Flags = line.xy[+u[+|-]unit][+p]

    Determine the shortest distance from the input data points to the line(s) given in the
    ASCII multisegment file line.xy.
    [`-L`](http://gmt.soest.hawaii.edu/doc/latest/mapproject.html#l)
- **N** : **geod2aux** : -- Str or [] --       Flags = [a|c|g|m]

    Convert from geodetic latitudes to one of four different auxiliary latitudes (longitudes are unaffected).
    [`-N`](http://gmt.soest.hawaii.edu/doc/latest/mapproject.html#n)
- **Q** : **list** : -- Str or [] --           Flags = [d|e]

    List all projection parameters. To only list datums, use Q=:d, to only list ellipsoids, use Q=:e.
    [`-Q`](http://gmt.soest.hawaii.edu/doc/latest/mapproject.html#q)
- **S** : **supress** : -- Bool or [] --

    Suppress points that fall outside the region.
    [`-S`](http://gmt.soest.hawaii.edu/doc/latest/mapproject.html#s)
- **T** : **change_datum** : -- Str --    Flags = [h]from[/to]

    Coordinate conversions between datums from and to using the standard Molodensky transformation.
    [`-T`](http://gmt.soest.hawaii.edu/doc/latest/mapproject.html#t)
- **W** : **map_size** : -- Str or [] --    Flags = [w|h]

    Prints map width and height on standard output. No input files are read.
    [`-W`](http://gmt.soest.hawaii.edu/doc/latest/mapproject.html#w)
- **Z** : **travel_times** : -- Str or Number --    Flags = [speed][+a][+i][+f][+tepoch]

    Calculate travel times along track as specified with -G.
    [`-Z`](http://gmt.soest.hawaii.edu/doc/latest/mapproject.html#z)

- $(GMT.opt_V)
- $(GMT.opt_b)
- $(GMT.opt_d)
- $(GMT.opt_e)
- $(GMT.opt_f)
- $(GMT.opt_g)
- $(GMT.opt_h)
- $(GMT.opt_i)
- $(GMT.opt_o)
- $(GMT.opt_p)
- $(GMT.opt_s)
- $(GMT.opt_swap_xy)
"""
function mapproject(cmd0::String="", arg1=[]; kwargs...)

	length(kwargs) == 0 && return monolitic("mapproject", cmd0, arg1)	# Speedy mode

	d = KW(kwargs)

	cmd, = parse_R("", d)
	cmd, = parse_J("", d)
	cmd = parse_V_params(cmd, d)
	cmd, = parse_b(cmd, d)
	cmd, = parse_d(cmd, d)
	cmd, = parse_e(cmd, d)
	cmd, = parse_f(cmd, d)
	cmd, = parse_g(cmd, d)
	cmd, = parse_h(cmd, d)
	cmd, = parse_i(cmd, d)
	cmd, = parse_o(cmd, d)
	cmd, = parse_p(cmd, d)
	cmd, = parse_s(cmd, d)
	cmd, = parse_swap_xy(cmd, d)

	cmd = add_opt(cmd, 'A', d, [:A :azim])
	cmd = add_opt(cmd, 'C', d, [:C :center])
	cmd = add_opt(cmd, 'D', d, [:D :override_units])
	cmd = add_opt(cmd, 'E', d, [:E :geod2ecef])
	cmd = add_opt(cmd, 'F', d, [:F :one2one])
	cmd = add_opt(cmd, 'G', d, [:G :track_distances])
	cmd = add_opt(cmd, 'I', d, [:I :inverse])
	cmd = add_opt(cmd, 'L', d, [:L :dist2line])
	cmd = add_opt(cmd, 'N', d, [:N :geod2aux])
	cmd = add_opt(cmd, 'Q', d, [:Q :list])
	cmd = add_opt(cmd, 'S', d, [:S :supress])
	cmd = add_opt(cmd, 'T', d, [:T :change_datum])
	cmd = add_opt(cmd, 'W', d, [:W :map_size])
	cmd = add_opt(cmd, 'Z', d, [:Z :travel_times])

	cmd, got_fname, arg1 = find_data(d, cmd0, cmd, 1, arg1)
	return common_grd(d, cmd, got_fname, 1, "mapproject", arg1)		# Finish build cmd and run it
end

# ---------------------------------------------------------------------------------------------------
mapproject(arg1=[], cmd0::String=""; kw...) = mapproject(cmd0, arg1; kw...)


text:
#############################
#
#   Some general functions so we furtehr only need 3D density
#
###################################################################

# a general function for cumulative projected mass
function projected_mass(rhofunc, rp, RMAX)
    rp = min(rp, RMAX)
    rp = max(rp, 1.0e-37)
    function circ_rho_func(r)
        θ = r<rp ? π/2 : asin(rp/r)
        rhofunc(r)*4*π*r*r*(1.0-cos(θ))
    end
    quadgk(circ_rho_func, 1.0e-37, RMAX)[1]
end

# a general function for cumulative mass
function cumulative_mass(rhofunc, r, RMAX)
    r = min(r, RMAX)
    r = max(r, 1.0e-37)
    quadgk(r->4.0*π*r*r*rhofunc(r), 1.0e-37, r)[1]
end

# a general function for potential
function potential(massfunc, r, RMAX)
    r = max(r, 1.0e-37)
    quadgk(r->G*massfunc(r)./r./r, 1.0e37, r)[1]
end

#############################
#
#   Sharply truncated ISOTHERMAL (@ R200)
#
###################################################################

function tiso_ρ(MX, r, RMAX)
    if r>RMAX
        return zero(r)
    end
    MX./4./π./RMAX./r./r
end

function tiso_m(MX, r, RMAX)
    r = min(r, RMAX)
    MX./RMAX.*r
end

tiso_mp(MX, rp, RMAX) =
    projected_mass(r->tiso_ρ(MX, r, RMAX), rp, RMAX)

#############################
#
#   Sharply truncated NFW (@ RX)
#
###################################################################

function tnfw_ρ(MX, RS, r, RMAX)
    RMAX
    if r>RMAX
        return zero(r)
    end
    rrX = RS+RMAX
    k = MX./(log(rrX./RS)-RMAX./rrX)
    ρ0 = k/4/π/RS/RS/RS
    x = r./RS
    ρ0./(x.*(1.0+x).^2)
end

function tnfw_m(MX, RS, r, RMAX)
    if r<1.0e-17
        return zero(r)
    end
    rrX = RS+RMAX
    k = MX./(log(rrX./RS)-RMAX./rrX)
    r = min(RMAX, r)
    rrs = RS+r
    (log(rrs./RS)-r./rrs).*k
end

tnfw_potential(MX, RS, r, RMAX) =
    potential(r->tnfw_m(MX,RS,r,RMAX), r, RMAX)

tnfw_mp(MX, RS, rp, RMAX) =
    projected_mass(r->tnfw_ρ(MX, RS, r, RMAX), rp, RMAX)

#############################
#
#   Exponentially truncated NFW (@ RX)
#
###################################################################

function lnfw_ρ(MX, RS, r, RMAX)
    rrX = RS+RMAX
    k = MX./(log(rrX./RS)-RMAX./rrX)
    ρ0 = k/4/π/RS/RS/RS
    x = r./RS
    ρ0./(x.*(1.0+x).^2) ./ (1.0+exp((r-RMAX)./RS))
end

lnfw_m(MX, RS, r, RMAX) =
    cumulative_mass(r->lnfw_ρ(MX, RS, r, RMAX), r, 100*RMAX)

lnfw_mp(MX, RS, rp, RMAX) =
    projected_mass(r->lnfw_ρ(MX, RS, r, RMAX), rp, 100*RMAX)

#############################
#
#   Void surrounded NFW
#
###################################################################

function vnfw_ρ(MX, RS, r, RMAX; vstart=2.0, vend=3.0)
    rrX = RS+RMAX
    k = MX./(log(rrX./RS)-RMAX./rrX)
    ρ0 = k/4/π/RS/RS/RS
    x = r./RS
    ρ0./(x.*(1.0+x).^2) ./ (1.0+exp((r-RMAX)./RS)) + RHO_CRIT*OM(a).*(-1./(1.0+exp(-(r-vstart*RMAX)./RS)) ./ (1.0+exp((r-vend*RMAX)./RS)))
end

vnfw_m(MX, RS, r, RMAX; vstart=2.0, vend=10.0) =
    cumulative_mass(r->vnfw_ρ(MX, RS, r, RMAX; vstart=vstart, vend=vend), r, 100*RMAX)

vnfw_mp(MX, RS, rp, RMAX; vstart=2.0, vend=10.0) =
    projected_mass(r->vnfw_ρ(MX, RS, r, RMAX; vstart=vstart, vend=vend), rp, 100*RMAX)

max_stars_repo_name: skariel/Lensing

max_issues_repo_name: skariel/Lensing

max_forks_repo_name: skariel/Lensing


text:
# ---
# title: 1599. Maximum Profit of Operating a Centennial Wheel
# id: problem1599
# author: Tian Jun
# date: 2021-06-01
# difficulty: Medium
# categories: Greedy
# link: <https://leetcode.com/problems/maximum-profit-of-operating-a-centennial-wheel/description/>
# hidden: true
# ---
#
# You are the operator of a Centennial Wheel that has **four gondolas** , and
# each gondola has room for **up** **to** **four people**. You have the ability
# to rotate the gondolas **counterclockwise** , which costs you `runningCost`
# dollars.
#
# You are given an array `customers` of length `n` where `customers[i]` is the
# number of new customers arriving just before the `ith` rotation (0-indexed).
# This means you **must rotate the wheel**`i` **times before the**`customers[i]`
# **customers arrive**. **You cannot make customers wait if there is room in the
# gondola**. Each customer pays `boardingCost` dollars when they board on the
# gondola closest to the ground and will exit once that gondola reaches the
# ground again.
#
# You can stop the wheel at any time, including **before** **serving** **all**
# **customers**. If you decide to stop serving customers, **all subsequent
# rotations are free** in order to get all the customers down safely. Note that
# if there are currently more than four customers waiting at the wheel, only
# four will board the gondola, and the rest will wait **for the next rotation**.
#
# Return _the minimum number of rotations you need to perform to maximize your
# profit._ If there is **no scenario** where the profit is positive, return
# `-1`.
#
#
#
# **Example 1:**
#
# ![](https://assets.leetcode.com/uploads/2020/09/09/wheeldiagram12.png)
#
#
#
#     Input: customers = [8,3], boardingCost = 5, runningCost = 6
#     Output: 3
#     Explanation: The numbers written on the gondolas are the number of people currently there.
#     1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * /$/5 - 1 * /$/6 = /$/14.
#     2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * /$/5 - 2 * /$/6 = /$/28.
#     3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * /$/5 - 3 * /$/6 = /$/37.
#     The highest profit was /$/37 after rotating the wheel 3 times.
#
# **Example 2:**
#
#
#
#     Input: customers = [10,9,6], boardingCost = 6, runningCost = 4
#     Output: 7
#     Explanation:
#     1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * /$/6 - 1 * /$/4 = /$/20.
#     2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * /$/6 - 2 * /$/4 = /$/40.
#     3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * /$/6 - 3 * /$/4 = /$/60.
#     4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * /$/6 - 4 * /$/4 = /$/80.
#     5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * /$/6 - 5 * /$/4 = /$/100.
#     6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * /$/6 - 6 * /$/4 = /$/120.
#     7. 1 boards, the wheel rotates. Current profit is 25 * /$/6 - 7 * /$/4 = /$/122.
#     The highest profit was /$/122 after rotating the wheel 7 times.
#
#
#
# **Example 3:**
#
#
#
#     Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92
#     Output: -1
#     Explanation:
#     1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * /$/1 - 1 * /$/92 = -/$/89.
#     2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * /$/1 - 2 * /$/92 = -/$/177.
#     3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * /$/1 - 3 * /$/92 = -/$/269.
#     4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * /$/1 - 4 * /$/92 = -/$/357.
#     5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * /$/1 - 5 * /$/92 = -/$/447.
#     The profit was never positive, so return -1.
#
#
# **Example 4:**
#
#
#
#     Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8
#     Output: 9
#     Explanation:
#     1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * /$/3 - 1 * /$/8 = /$/4.
#     2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * /$/3 - 2 * /$/8 = /$/8.
#     3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * /$/3 - 3 * /$/8 = /$/12.
#     4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * /$/3 - 4 * /$/8 = /$/16.
#     5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * /$/3 - 5 * /$/8 = /$/20.
#     6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * /$/3 - 6 * /$/8 = /$/24.
#     7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * /$/3 - 7 * /$/8 = /$/28.
#     8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * /$/3 - 8 * /$/8 = /$/32.
#     9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * /$/3 - 9 * /$/8 = /$/36.
#     10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * /$/3 - 10 * /$/8 = /$/31.
#     The highest profit was /$/36 after rotating the wheel 9 times.
#
#
#
#
# **Constraints:**
#
#   * `n == customers.length`
#   * `1 <= n <= 105`
#   * `0 <= customers[i] <= 50`
#   * `1 <= boardingCost, runningCost <= 100`
#
#
## @lc code=start
using LeetCode

function min_operations_max_profit(customers::Vector{Int}, boarding_cost::Int, running_cost::Int)
    prof, maxp, res, wait_cnt = 0, 0, -1, 0
    turn = 1
    while wait_cnt != 0 || turn <= length(customers)
        wait_cnt += turn <= length(customers) ? customers[turn] : 0
        up_cnt = min(wait_cnt, 4)
        wait_cnt -= up_cnt
        prof += up_cnt * boarding_cost - running_cost
        if prof > maxp
            maxp = prof
            res = turn
        end
        turn += 1
    end
    return res
end
## @lc code=end


text:
for dtype in ("", "Dist")
    for (elty, ext) in ((:Float32, :s),
                        (:Float64, :d),
                        (:ComplexF32, :c),
                        (:ComplexF64, :z))
        mat = Symbol(dtype, "Matrix")
        @eval begin
            function inverse!(A::$mat{$elty})
                ElError(ccall(($(string("ElInverse", dtype, "_", ext)), libEl), Cuint,
                    (Ptr{Cvoid},),
                    A.obj,))
                return A
            end
        end
    end
end