---
# System prepended metadata

title: '題解 zerojudge h081: 1. 程式交易'
tags: [APCS, c++]

---

###### tags: `APCS` `c++`
# 題解 zerojudge h081: 1. 程式交易
```cpp=
#include<iostream>
using namespace std;

int main(){
    int n,d;
    cin>>n>>d;

    int a,x=0,x1=300,y;
    int ans=0;
    for(int i=0;i<n;i++){
        cin>>a;
        if(x==0) {
            if(a<=(x1-d)){x=a;}
        }

        else{
            if(a>=(x+d)){ans+=(a-x);x=0;x1=a;}
        }
    }
    cout<<ans;
}

```